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.
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.
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.
You can use TimeZoneInfo for this
DateTime eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "Eastern Standard Time");
Replace with the desired time zone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With