C#: Making sure DateTime.Now returns a GMT + 1 time

c#, datetime, timezone

Solution

Use the TimeZoneInfo class found in System.Core;

You must set the DateTimeKind to DateTimeKind.Utc for this.

DateTime MyTime = new DateTime(1990, 12, 02, 19, 31, 30, DateTimeKind.Utc);

DateTime MyTimeInWesternEurope = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(MyTime, "W. Europe Standard Time");

Only if you're using .Net 3.5 though!

Problem

I am using `DateTime.Now` to show something according to today's date, and when working locally (Malta, Europe) the times appear correctly (obviously because of the Time Zone) but ofcourse when I upload it to my hosting server (USA), `DateTime.Now` does not represent the correct time zone. Therefore, in my code, how can I convert `DateTime.Now` to correctly return the time from a GMT + 1 timezone ?

Original source