ScArcher2
ScArcher2

Reputation: 87227

How do I format a DateTime structure so that it matches the format used by DataContractJsonSerializer?

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

Upvotes: 2

Views: 1171

Answers (1)

Andrew Savinykh
Andrew Savinykh

Reputation: 26300

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?

Upvotes: 1

Related Questions