How to get time elsewhere programmatically in .NET?

How can I get the time elsewhere in UTC, or at least what UTC time is set on the computer , so I can increase / decrease the current time and get what I want?

My specific problem:

I am in a specific place and I want to know what time is in the UK .

+4
source share
1 answer

I finally found the answer. In this article, we explain TimeZoneInfo and TimeZone in detail, and there are some examples of converting time at different times.

All you need to know is the identifier of the time zone in which you want to convert.

Example (taken from the site):

 DateTimeOffset nowDateTime = DateTimeOffset.Now; DateTimeOffset newDateTime = TimeZoneInfo.ConvertTime( nowDateTime, TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time")); Console.WriteLine("Now: {0}", nowDateTime); Console.WriteLine("Now in Hawaii: {0}", newDateTime); 

prints

Now: 3/5/2011 6:30:48 PM -08:00
Now in Hawaii: 3/5/2011 4:30:48 PM -10:00

To get a list of all identifiers, you can:

  • Check HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
  • Request TimeZoneInfo.GetSystemTimeZones();
+11
source

Source: https://habr.com/ru/post/1342461/


All Articles