I noticed this strange issue regarding .ToUniversalTime(), but I can't figure out why.
If I do
DateTime currentServerTime = System.DateTime.Now;
DateTime currentUTCServerTime = currentServerTime.ToUniversalTime();
log.Debug("Current server time :" + currentServerTime);
log.Debug("Current Server UTC Time is :" + currentUTCServerTime);
Result
Current server time :2/18/2014 2:09:23 PM
Current Server UTC Time is :2/18/2014 7:09:23 PM
5 hours apart.
Given the server is located on U.S East Coast, with -5 offset. That's correct.
Now if I do
String dateTimeString = "7/10/2013";
DateTime baseLined = Convert.ToDateTime(dateTimeString);
DateTime convertedUTCTime = baseLined.ToUniversalTime();
log.Debug(dateTimeString + " is :"+baseLined);
log.Debug(dateTimeString + " UTC time is :" + convertedUTCTime);
Result
7/10/2013 is : 7/10/2013 12:00:00 AM
7/10/2013 UTC time is: 7/10/2013 4:00:00 AM
4 hours apart.
Why one result shows 5 hours difference and another shows 4????
Please help.
=== Edit ====
Thanks to Jon and Usr. Now I understand .ToUniversalTime() will take DST of the server into consideration and adjust the UTC according to the DateTime object it attaches to.
So I'm still trying to wrap my head around it for this situation.
In my database, 7/10/2013 is a due date and was recorded as "7/10/2013 4:00:00 AM" (Since during that time, it was during daylight saving, offset -4).
Now it's in Feburary 2014, DST is not in effect, offset is now -5. But because of this, when I adjust user's timezone offset -5 to "7/10/2013 4:00:00 AM", it becomes 7/09/2013 instead of 7/10/2013.
How do I handle this situation?
Because of daylight savings. Local time is as it appears on your clock. UTC is continuous without gaps, without ambiguous points in time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With