How to convert line offset in timepan in c #

I am trying to convert the conversion time to the user’s time zone, but I don’t have a Windows time zone line, such as “Standard Pacific Time”. All I have is a string offset, such as "-07: 00". Looks like I need to create a time span. Is this the only way to parse this line manually ?. It seems like there should be a way to convert time using string offset, but maybe I am missing something.

I have this, but this requires a time zone. I am trying to change it to use an offset instead, but you can see the time span that was created for the conversion, and I need to get my offset in the time span.

static void Main(string[] args) { var currentTimeInPacificTime = ConvertUtcTimeToTimeZone(DateTime.UtcNow, "Pacific Standard Time"); //TimeSpan ts = new TimeSpan("-07:00"); Console.ReadKey(); } static DateTimeOffset ConvertUtcTimeToTimeZone(DateTime dateTime, string toTimeZoneDesc) { if (dateTime.Kind != DateTimeKind.Utc) throw new Exception("dateTime needs to have Kind property set to Utc"); TimeSpan toUtcOffset = TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc).GetUtcOffset(dateTime); var convertedTime = DateTime.SpecifyKind(dateTime.Add(toUtcOffset), DateTimeKind.Unspecified); return new DateTimeOffset(convertedTime, toUtcOffset); } 
+4
source share
4 answers

You can simply use the TimeSpan.Parse method:

 TimeSpan ts = TimeSpan.Parse("-07:00"); Console.WriteLine(ts); // -07:00:00 

Or, if you want to be a little more secure, try the TimeSpan.TryParse method:

 TimeSpan ts; if (TimeSpan.TryParse("-07:00", out ts)) Console.WriteLine(ts); // -07:00:00 

But of course, if all you want to do is convert the UTC date / time to a local date / time, you can simply do this:

 DateTime localDateTime = utcDateTime.ToLocalTime(); 

Or convert it to any time zone:

 TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc); DateTime localDateTime = TimeZoneInfo.ConvertTime(utcDateTime, tzi); 
+5
source

For more complex / custom formats, you can also use TimeSpan.ParseExact(String, String, IFormatProvider) , where the second line is a Custom TimeSpan format string .

API information is available at msdn.microsoft.com and is associated with the above .linked.

+1
source

I am trying to convert the conversion time to the user’s time zone, but I don’t have a Windows time zone line, such as “Standard Pacific Time”. All I have is a string offset, such as "-07: 00".

Then you do not have what it takes to convert correctly. Read "Time Zone! = Offset" in the time zone time wiki .

It is important to understand that the value of "Pacific Standard Time" is the .Id for the TimeZoneInfo object that is used for US Pacific Time. It covers both Pacific Standard Time (UTC-8) and Pacfic Daytime (UTC-7).

All I have is a string offset, such as "-07: 00". Looks like I need to create a time span.

Now you have the so-called XY problem . You should not need to work with the offset yourself.

Your code has a call to dateTime.Add(toUtcOffset) . When converting time zones, it’s the smell of code that you are doing it wrong. You never have to manually add or subtract time to manipulate time zones. This should be reserved for actually changing the point in time you are talking about.

What you should do is collect the real-time identifier from the user. Either "Pacific Standard Time" for use with TimeZoneInfo , or "America/Los_Angeles" for use with the TZDB implementation, such as Noda Time .

If conversions in the time zone are not important in your context, you might just want to collect the full DateTimeOffset value, for example 2013-08-17T13:27:00.000-07:00 .

0
source

There are timezone lines that include Standard Pacific Time . A complete list can be found here. http://www.xiirus.net/articles/article-_net-convert-datetime-from-one-timezone-to-another-7e44y.aspx

Any DateTime object can be converted to some time zone -

  TimeZoneInfo timeZoneInfo; DateTime dateTime ; //Set the time zone information to Pacific Standard Time timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); //Get date and time in US Mountain Standard Time dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo); //Print out the date and time Console.WriteLine(dateTime.ToString("yyyy-MM-dd HH-mm-ss")); 

So your method can be changed as -

 static DateTimeOffset ConvertUtcTimeToTimeZone(DateTime dateTime, string toTimeZoneDesc) { return new DateTimeOffset(TimeZoneInfo.ConvertTime(dateTime, TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc))); } 
-one
source

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


All Articles