Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set global timezone in .net core

In .Net Core 2 -

Is there a way to set the application's timezone globally so that whenever I request for DateTime.Now I'll get the current time for a timezone I want (lets say GMT+3) instead of the time of the server where the application is hosted?

I figured a good place to have a configuration providing this would be the Startup.cs file, but my search queries gets me nowhere with this.

like image 631
shahaf Avatar asked Feb 21 '19 02:02

shahaf


People also ask

How do I get different time zones in C#?

To display all time zones in our current system, we use TimeZoneInfo. GetSystemTimeZone() static method that returns all available time zones on a machine. The DisplayName property is the name of the time zone.

Does C# DateTime have a timezone?

DateTime itself contains no real timezone information. It may know if it's UTC or local, but not what local really means. DateTimeOffset is somewhat better - that's basically a UTC time and an offset.


1 Answers

You can use TimeZoneInfo for this

DateTime eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "Eastern Standard Time");

Replace with the desired time zone.

like image 164
Thomas Ingle Avatar answered Sep 19 '22 21:09

Thomas Ingle