2

When I serialize the current time as a DateTime using the DataContractJsonSerializer I get the following 1307654074638-0500.

I'd like to generate my own JSON that could be deserialized by the DataContractJsonSerializer, but I can't figure out how to generate a DateTime with the same format.

I believe it's using the number of milliseconds since the epoch as it's base, but I keep getting numbers that are off by around 6 hours. I believe this has something to do with the Timezone (I'm in Central Time) which would make sense.

var myDate = DateTime.Now; var epoch = new DateTime(1970, 01, 01, 01, 0, 0); var serializer = new DataContractJsonSerializer(typeof (DateTime)); var ms = new MemoryStream(); serializer.WriteObject(ms, myDate); Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); Console.WriteLine((myDate.AddTicks(-epoch.Ticks)).Ticks / 10000); 

outputs

"\/Date(1307654587318-0500)\/" 1307632987318 

or for easier comparison

1307654587318-0500 1307632987318 
4
  • Is the issue with the serialization or that you are not using DateTime.UtcNow? Commented Jun 9, 2011 at 21:28
  • msdn.microsoft.com/en-us/library/…? Commented Jun 9, 2011 at 21:30
  • It's still off even if i use UtcNow. Commented Jun 9, 2011 at 21:34
  • Can't you use DataContractJsonSerializer for generating DateTime in the required format? Commented Jun 9, 2011 at 21:48

1 Answer 1

1

I suggest that you use DataContractJsonSerializer class for getting the data in the format you need. Failing that, you can do something along these lines:

using System; using System.Globalization; using System.IO; using System.Runtime.Serialization.Json; using System.Text; namespace SO6299496 { class Program { static void Main() { var myDate = DateTime.Now; var serializer = new DataContractJsonSerializer(typeof(DateTime)); var ms = new MemoryStream(); serializer.WriteObject(ms, myDate); Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); Console.WriteLine(SerializeDate(myDate)); } static string SerializeDate(DateTime value) { var epoch = new DateTime(1970, 01, 01, 01, 0, 0); long date = (value.ToUniversalTime().Ticks - epoch.Ticks) / 10000; string sign = ""; string offset = ""; if (value.Kind == DateTimeKind.Unspecified || value.Kind == DateTimeKind.Local) { TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime()); sign = utcOffset.Ticks >= 0L ? "+" : "-"; int offsetHours = Math.Abs(utcOffset.Hours); int offsetMinutes = Math.Abs(utcOffset.Minutes); offset = (offsetHours < 10) ? ("0" + offsetHours) : offsetHours.ToString(CultureInfo.InvariantCulture); offset += (offsetMinutes < 10) ? ("0" + offsetMinutes) : offsetMinutes.ToString(CultureInfo.InvariantCulture); } return date + sign + offset; } } } 

But I'd like to re-iterate, that you are better off using DataContractJsonSerializer. Why invent something that is already invented?

Sign up to request clarification or add additional context in comments.

1 Comment

I kept going down the road of trying to format the date. The DataContractJsonSerializer works fine for this. I just never considered using it directly to serialize a single field.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.