Time Zones and Servers


Wonder-121-Time-Zone-Static-ImageI am getting married and that means I get a bunch of development tasks to do for the wedding planning. I guess it’s my own fault, I did propose with an app.

One of the tasks I had to do was create a new page on my wedding site for the day of the wedding to include things like directions and parking. Pretty simple HTML stuff, but one thing I wanted to be sure of was to only show the page on the day of the wedding. This should be easy, but the time zone of the server has kicked my ass before.

The problem is that if I check for the date, the date might get shifted if the time is after midnight where the server is. Luckily the TimeZoneInfo class makes this pretty easy in C#. The trick is to first get the time zone you care about:

var tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

Once I have the time zone, I can convert the current time to my time zone:

var today = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tz);

Lastly, I can test the date portion of the DateTime to make sure it’s the right date:

if (today.Date == new DateTime(2014, 05,31))
{
  return RedirectToAction("DayOfWedding");
}

Easy, right?


Ready to Learn Vue with ASP.NET Core?

My new Wilder Minds' course is available as an Early Access for only $79. It will be released on a weekly basis. The first module is now available:

Enroll Today


Shawn
Shawn Wildermuth
Author, Teacher, and Coach




My Courses

Wilder Minds Training
Vue.js by Example (Now Available)
Bootstrap 4 by Example
Intro to Font Awesome 5 (Free Course)
Building a Web App with ASP.NET Core, MVC6, EF Core, Bootstrap and Angular (updated for 2.1)
Using Visual Studio Code for ASP.NET Core Projects
Implementing ASP.NET Web API
Web API Design
JavaScript for C# Developers

Application Name WilderBlog Environment Name Production
Application Ver v4.0.30319 Runtime Framework x86
App Path D:\home\site\wwwroot\ Runtime Version .NET Core 4.6.26725.06
Operating System Microsoft Windows 10.0.14393 Runtime Arch X86