C # and current local time for different countries

C # offers a way to get the current date on DateTime.Now . But the problem is that my server is in the USA and I get time in the USA when I use DateTime.Now

Is there a way to get the current local time for a specific country in C #, since my users are from all over the world? (I have a locale for each user, so if I can get the current time for each locale, then I'm sure)

I saw a method using the time zone , but then I need to save the time zone and the link to the country.

I searched and most of the questions here are about converting UTC time to local time and vice versa. Hope someone can enlighten me.

+6
source share
2 answers

Save the time on your server using UTC time (DateTime.UtcNow) , and then depending on the time zone of users, you change this utc time. Never save local time in db

+4
source

Just look at the TimeZoneInfo class. You should be able to convert to local and server time and back, supporting DST (daylight saving time).

 DateTimeOffset newTime = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("AppTimeZone")); 

You can also find Extraordinary Open Source Time Zone by one of the Great John Skeet Noda Time

+2
source

All Articles