shahaf
shahaf

Reputation: 781

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.

Upvotes: 13

Views: 26786

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241959

No.

DateTime.Now always returns the time in the system's local time zone.

Upvotes: 5

Thomas Ingle
Thomas Ingle

Reputation: 121

You can use TimeZoneInfo for this

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

Replace with the desired time zone.

Upvotes: 12

Related Questions