Answered by:
ToUniversalTime strange behavior

Question
-
Hi. ToUniversalTime() returns incorrect DateTime for (2013, 3, 17, 1, 0, 0).
see my code:
//d1 is less than d2 how ever after converting to utc //resault is strange and d1Utc is greater than d2Utc DateTime d1 = new DateTime(2013, 3, 17, 0, 45, 0); DateTime d2 = new DateTime(2013, 3, 17, 1, 0, 0); var d1Utc = d1.ToUniversalTime(); var d2Utc = d2.ToUniversalTime(); Console.WriteLine(d1Utc.ToString()); //3/16/2013 9:15:00 PM Console.WriteLine(d2Utc.ToString()); //3/16/2013 8:30:00 PM
Tanks in advance
- Edited by Zarei Ramin Tuesday, March 6, 2012 7:17 PM
Tuesday, March 6, 2012 7:15 PM
Answers
-
As I double checked, I found that 2013, 3, 17, 1, 0, 0 is in daylight saving time range for the Iran +3:30 GMT time zone.
http://msdn.microsoft.com/en-us/library/system.datetime.isdaylightsavingtime.aspx
So we need handle it like the below code for your reference:
if (d2.IsDaylightSavingTime()) { var d2Utc = d2.ToUniversalTime().AddHours(1); }
Hope it helps.Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Zarei Ramin Monday, March 12, 2012 7:15 AM
Monday, March 12, 2012 5:33 AM
All replies
-
What timezone are you in and when's daylight saving kicking in for you?
Check out: http://msdn.microsoft.com/en-us/library/bb396403.aspx to see if the DateTime value you have is Ambiguous in your timezone (might explain the 1hr skew)
My blog: blog.jessehouwing.nl
- Edited by Jesse HouwingMVP Tuesday, March 6, 2012 9:12 PM
Tuesday, March 6, 2012 9:07 PM -
Hi Jesse, My PC time zone is Iran +3:30 GMT.
I'll check it tomorrow and post results here.
tanks again.
Tuesday, March 6, 2012 9:20 PM -
Hi. Jesse.
TimeZoneInfo iranZone = TimeZoneInfo.FindSystemTimeZoneById("Iran Standard Time"); Console.WriteLine(iranZone.IsAmbiguousTime(d1)); // false Console.WriteLine(iranZone.IsAmbiguousTime(d2)); // false
I've checked both dates above. they are InAmbigoues!!
Now I'm wondering where is the problem?
Tanks.
Wednesday, March 7, 2012 6:04 PM -
It beats me :). Given a date in the second or third week of March chances where high Daylight Savings had something to do with it...
My blog: blog.jessehouwing.nl
Wednesday, March 7, 2012 10:04 PM -
I have connected the Microsoft Connect Site to help you submit a feedback.
Link at here:
For now, We are doing some research of this issue and will get back to you once we have any update information.
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Friday, March 9, 2012 2:49 AM -
As I double checked, I found that 2013, 3, 17, 1, 0, 0 is in daylight saving time range for the Iran +3:30 GMT time zone.
http://msdn.microsoft.com/en-us/library/system.datetime.isdaylightsavingtime.aspx
So we need handle it like the below code for your reference:
if (d2.IsDaylightSavingTime()) { var d2Utc = d2.ToUniversalTime().AddHours(1); }
Hope it helps.Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Zarei Ramin Monday, March 12, 2012 7:15 AM
Monday, March 12, 2012 5:33 AM -
Tanks Mr. Rocky Yue. your solution is right.Monday, March 12, 2012 7:15 AM
-
You are welcome.
It's my pleasure to help you.
Best Regards,
Rocky Yue[MSFT]
MSDN Community Support | Feedback to us
Monday, March 12, 2012 7:18 AM