12

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.

shahaf
  • 741
  • 1
  • 8
  • 26

2 Answers2

10

You can use TimeZoneInfo for this

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

Replace with the desired time zone.

Thomas Ingle
  • 101
  • 3
  • 5
    Be careful with cross-platform issues: https://devblogs.microsoft.com/dotnet/cross-platform-time-zones-with-net-core/ – JustAMartin Oct 23 '19 at 12:49
6

No.

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

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575