0

In my code, I am getting Date value as string (reading from xml doc) where no time zone is specified. Here is the sample code...

string dateStr = "2012-06-23";
DateTime convertedDate = DateTime.Parse(dateStr).ToUniversalTime();

When I check the value of convertedDate, it is "22/06/2012 18:30:00". I want to know how exectly ToUniversalTime() method works in this case and how it concluded dateTime format in this case.

Thanks in advance.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Abhash786
  • 881
  • 2
  • 24
  • 53
  • 3
    Related: http://stackoverflow.com/questions/1201378/how-does-datetime-touniversaltime-work – Soner Gönül Aug 28 '14 at 07:24
  • 1
    A guess: Parse returs DateTime with unknown zone (neither local nor universal) and `ToUniversalTime` assumes that it is local (when unspecified) – firda Aug 28 '14 at 07:25
  • @firda [Link](http://msdn.microsoft.com/en-us/library/system.datetime.parse.aspx) to the two [documentation](http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx) pages and you've got yourself an answer. – Rawling Aug 28 '14 at 07:34

1 Answers1

1

The Parse method sets the Kind property of the date to DateTimeKind.Unspecified as there is no time zone information in the string. The ToUniversalTime method assumes that the time is local and converts it to UTC.

Ref: DateTime.Parse Method

"Generally, the Parse method returns a DateTime object whose Kind property is DateTimeKind.Unspecified."

Ref: DateTime.ToUniversalTime Method

"Unspecified: The current DateTime object is assumed to be a local time, and the conversion is performed as if Kind were Local."

Guffa
  • 687,336
  • 108
  • 737
  • 1,005