Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would use my <a href="http://nodatime.org/" rel="nofollow noreferrer">Noda Time</a> project personally. (Admittedly I'm biased as the author, but it would be cleaner...) But if you can't do that...</p> <p>Either use <code>DateTime.ParseExact</code> specifying the exact format you expect, and include <code>DateTimeStyles.AssumeUniversal</code> and <code>DateTimeStyles.AdjustToUniversal</code> in the parse code:</p> <pre><code>using System; using System.Globalization; class Test { static void Main() { var date = DateTime.ParseExact("2012-09-30T23:00:00.0000000Z", "yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); Console.WriteLine(date); Console.WriteLine(date.Kind); } } </code></pre> <p>(Quite why it would adjust to local by default without <code>AdjustToUniversal</code> is beyond me, but never mind...)</p> <p>EDIT: Just to expand on my objections to mattytommo's suggestion, I aimed to prove that it would lose information. I've failed so far - but in a very peculiar way. Have a look at this - running in the Europe/London time zone, where the clocks go back on October 28th in 2012, at 2am local time (1am UTC):</p> <pre><code>DateTime local1 = DateTime.Parse("2012-10-28T00:30:00.0000000Z"); DateTime local2 = DateTime.Parse("2012-10-28T01:30:00.0000000Z"); Console.WriteLine(local1 == local2); // True DateTime utc1 = TimeZoneInfo.ConvertTimeToUtc(local1); DateTime utc2 = TimeZoneInfo.ConvertTimeToUtc(local2); Console.WriteLine(utc1 == utc2); // False. Hmm. </code></pre> <p>It looks like there's a "with or without DST" flag being stored <em>somewhere</em>, but I'll be blowed if I can work out where. The docs for <a href="http://msdn.microsoft.com/en-us/library/bb495915.aspx" rel="nofollow noreferrer"><code>TimeZoneInfo.ConvertTimeToUtc</code></a> state</p> <blockquote> <p>If <em>dateTime</em> corresponds to an ambiguous time, this method assumes that it is the standard time of the source time zone.</p> </blockquote> <p>That doesn't <em>appear</em> to be the case here when converting <code>local2</code>...</p> <p>EDIT: Okay, it gets even stranger - it depends which version of the framework you're using. Consider this program:</p> <pre><code>using System; using System.Globalization; class Test { static void Main() { DateTime local1 = DateTime.Parse("2012-10-28T00:30:00.0000000Z"); DateTime local2 = DateTime.Parse("2012-10-28T01:30:00.0000000Z"); DateTime utc1 = TimeZoneInfo.ConvertTimeToUtc(local1); DateTime utc2 = TimeZoneInfo.ConvertTimeToUtc(local2); Console.WriteLine(utc1); Console.WriteLine(utc2); DateTime utc3 = local1.ToUniversalTime(); DateTime utc4 = local2.ToUniversalTime(); Console.WriteLine(utc3); Console.WriteLine(utc4); } } </code></pre> <p>So this takes two <em>different</em> UTC values, parses them with <code>DateTime.Parse</code>, then converts them back to UTC in two different ways.</p> <p>Results under .NET 3.5:</p> <pre><code>28/10/2012 01:30:00 // Look - we've lost information 28/10/2012 01:30:00 28/10/2012 00:30:00 // But ToUniversalTime() seems okay... 28/10/2012 01:30:00 </code></pre> <p>Results under .NET 4.5 beta:</p> <pre><code>28/10/2012 00:30:00 // It's okay! 28/10/2012 01:30:00 28/10/2012 00:30:00 28/10/2012 01:30:00 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload