Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 230
Andreas Grech Avatar asked Jul 10 '09 09:07

Andreas Grech


1 Answers

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!

like image 117
Lloyd Powell Avatar answered Nov 15 '22 17:11

Lloyd Powell