tags:

views:

28

answers:

1
+1  Q: 

timespan and UTC

I have a Timespan variable, which has time in local time zone but for a database ( cold type time) I need to pass UTC . How do I do this? Also I need to get UTC to Local time to populate Timespan variable on pageLoad. Thanks!!!

+1  A: 

I suppose I would load the TimeSpan into a DateTime, then get the universal time from the DateTime and convert it back again.

var dt = new DateTime( timeSpan.Ticks );
var utc = dt.ToUniversalTime();
chaiguy
Following won't convert 8PM eastern correctly to UTC Dim dt As New DateTime(time.Ticks) dt = dt.ToUniversalTime() Dim dtUniversal As New TimeSpan(dt.Ticks)
Are the ticks representing different things perhaps? Different systems use different tick representations--usually the date to start counting milliseconds from. If you can convert your timespan into specific values for year, month, day, etc. you can build a DateTime from those values and then convert it to UTC.
chaiguy
As you suggested, How do I build datetime? All I am storing in the database is hour and default minutes and seconds are alwyas 00. eg. My value stored is 01:00:00 how do I convert this to dateTime? - thanks
I will accept this one I made few changes but it was a good starting point for me - thanks