set global timezone in .net core

15,764

Solution 1

You can use TimeZoneInfo for this

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

Replace with the desired time zone.

Solution 2

No.

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

Share:
15,764
shahaf
Author by

shahaf

Updated on June 25, 2022

Comments

  • shahaf
    shahaf almost 2 years

    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.

  • JustAMartin
    JustAMartin over 4 years
    Be careful with cross-platform issues: devblogs.microsoft.com/dotnet/…