Time and UTC

I have a Timespan variable that has time in the local time zone, but for the database (cold type time) I need to go through UTC. How can I do it? I also need to get UTC at Local time in order to populate the Timespan variable on the Load page. Thanks!!!

+6
source share
3 answers

I assume that I load the TimeSpan into a DateTime, and then get the universal time from DateTime and convert it again.

var dt = new DateTime( timeSpan.Ticks ); var utc = dt.ToUniversalTime(); 
+10
source share
 TimeSpan LocalTimeToUTCTime(TimeSpan localTime) { var dt = new DateTime(localTime.Ticks); var utc = dt.ToUniversalTime(); return new TimeSpan(utc.Ticks); } 
+3
source share
 class TimeConversion { public static TimeSpan LocalTimeSpanToUTC(TimeSpan ts) { DateTime dt = new DateTime(ts.Ticks).AddDays(1); DateTime dtUtc = dt.ToUniversalTime(); TimeSpan tsUtc = dtUtc.TimeOfDay; return tsUtc; } public static TimeSpan UTCTimeSpanToLocal(TimeSpan tsUtc) { DateTime dtUtc = new DateTime(tsUtc.Ticks).AddDays(1); DateTime dt = dtUtc.ToLocalTime(); TimeSpan ts = dt.TimeOfDay; return ts; } } 
+2
source share

All Articles