Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string offset to timespan in c#

I'm trying to convert the convert time to the user's time zone, but I don't have the windows time zone string such as "Pacific Standard Time". All I have is a string offset such as "-07:00". Looks like I need to create a timespan. Is the only way to parse this string manually?. Seems like there should be a way to convert a time using a string offset, but maybe I am missing something.

I have this but it requires the timezone. I'm trying to modify it to use the offset instead, but you can see the timespan that is created for the conversion and I need to get my offset to the timespan.

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);
}
like image 864
KingOfHypocrites Avatar asked Aug 17 '13 19:08

KingOfHypocrites


People also ask

How do I convert DateTimeOffset to local time?

In performing the conversion to local time, the method first converts the current DateTimeOffset object's date and time to Coordinated Universal Time (UTC) by subtracting the offset from the time. It then converts the UTC date and time to local time by adding the local time zone offset.

What does DateTimeOffset mean?

The DateTimeOffset structure represents a date and time value, together with an offset that indicates how much that value differs from UTC. Thus, the value always unambiguously identifies a single point in time.

Should you use DateTimeOffset?

DateTime values lack any knowledge of time zone, or lack thereof. If you need to know when things actually occurred, with more precision than just the approximate date, and you can't be 100% sure that your dates are ALWAYS stored in UTC, then you should consider using DateTimeOffset to represent your datetime values.


Video Answer


2 Answers

You can just use the TimeSpan.Parse method:

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

Be careful to strip a leading "+" as TimeSpan.Parse will fail here. "+01:00" is incorrect, but "01:00" works.

Or if you want be a little more safe, 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 a UTC date/time to a local date/time, you can just do this:

DateTime localDateTime = utcDateTime.ToLocalTime();

Or to convert it to any timezone:

TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc);
DateTime localDateTime = TimeZoneInfo.ConvertTime(utcDateTime, tzi);
like image 87
p.s.w.g Avatar answered Oct 01 '22 16:10

p.s.w.g


For more complicated/non-standard formats you can also use TimeSpan.ParseExact(String, String, IFormatProvider), where the second String is a Custom TimeSpan Format String.

API information is available at msdn.microsoft.com, and is linked above.linked.

like image 34
Andrew Ring Avatar answered Oct 01 '22 18:10

Andrew Ring