rmh rmh - 1 year ago 104
C# Question

How to parse Timezone Offset string?

I retrieve a value from my database of a timeZone offset. The value that I get is in time.

For example, it could be

"-5:00"
,
"+7:30"
,
"+3:00"
, etc.

How do I convert that to a double so I can do an
AddHours()
call on a
DateTime
object?

Answer Source

Use the TimeSpan.Parse method:

var time = "+7:30";
time = time.Replace("+", "");  // Remove the + if it is there.

var hours = TimeSpan.Parse(time).TotalHours;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download