Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>DataContractJsonSerializer</code> will output the timezone portion (+zzzz) if your DateTime.Kind is equal to Local OR Unspecified. This behaviour differs from the XmlSerializer which only outputs the timezone portion if Kind equals Unspecified.</p> <p>If curious check out the source for <code>JsonWriterDelegator</code> which contains the following method:</p> <pre><code> internal override void WriteDateTime(DateTime value) { // ToUniversalTime() truncates dates to DateTime.MaxValue or DateTime.MinValue instead of throwing // This will break round-tripping of these dates (see bug 9690 in CSD Developer Framework) if (value.Kind != DateTimeKind.Utc) { long tickCount = value.Ticks - TimeZone.CurrentTimeZone.GetUtcOffset(value).Ticks; if ((tickCount &gt; DateTime.MaxValue.Ticks) || (tickCount &lt; DateTime.MinValue.Ticks)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.JsonDateTimeOutOfRange), new ArgumentOutOfRangeException("value"))); } } writer.WriteString(JsonGlobals.DateTimeStartGuardReader); writer.WriteValue((value.ToUniversalTime().Ticks - JsonGlobals.unixEpochTicks) / 10000); switch (value.Kind) { case DateTimeKind.Unspecified: case DateTimeKind.Local: // +"zzzz"; TimeSpan ts = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime()); if (ts.Ticks &lt; 0) { writer.WriteString("-"); } else { writer.WriteString("+"); } int hours = Math.Abs(ts.Hours); writer.WriteString((hours &lt; 10) ? "0" + hours : hours.ToString(CultureInfo.InvariantCulture)); int minutes = Math.Abs(ts.Minutes); writer.WriteString((minutes &lt; 10) ? "0" + minutes : minutes.ToString(CultureInfo.InvariantCulture)); break; case DateTimeKind.Utc: break; } writer.WriteString(JsonGlobals.DateTimeEndGuardReader); } </code></pre> <p>I've run the following test on my machine</p> <pre><code>var jsonSerializer = new DataContractJsonSerializer(typeof(DateTime)); var date = DateTime.UtcNow; Console.WriteLine("original date = " + date.ToString("s")); using (var stream = new MemoryStream()) { jsonSerializer.WriteObject(stream, date); stream.Position = 0; var deserializedDate = (DateTime)jsonSerializer.ReadObject(stream); Console.WriteLine("deserialized date = " + deserializedDate.ToString("s")); } </code></pre> <p>which produces the expected output:</p> <pre><code>original date = 2011-04-19T10:24:39 deserialized date = 2011-04-19T10:24:39 </code></pre> <p>Thus at some point your Date must be Unspecified or Local.</p> <p>After pulling it out of the DB convert the kind from Unspecified to Utc by calling </p> <pre><code> entity.Date = DateTime.SpecifyKind(entity.Date, DateTimeKind.Utc); </code></pre> <p>and don't forget to assign the return value of <code>SpecifyKind</code> back into your object like I have</p>
 

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