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!!!
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();
TimeSpan LocalTimeToUTCTime(TimeSpan localTime) { var dt = new DateTime(localTime.Ticks); var utc = dt.ToUniversalTime(); return new TimeSpan(utc.Ticks); }
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; } }