Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6009041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:54:50+00:00 2026-05-23T01:54:50+00:00

When I serialize the current time as a DateTime using the DataContractJsonSerializer I get

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:54:51+00:00Added an answer on May 23, 2026 at 1:54 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to serialize a Type object in the following way: Type myType =
I would like to serialize and deserialize objects without having to worry about the
Is it possible to serialize and deserialize a class in C++? I've been using
When I serialize; public class SpeedDial { public string Value { get; set; }
I'd like to serialize some LINQ generated objects and store them in a table
I plan to serialize a Django model to XML when it's saved or updated.
I am able to serialize an object into a file and then restore it
I need to serialize a huge amount of data (around 2gigs) of small objects
I am trying to serialize an object to a sql compact database. I am
I need to serialize/de-serialize some objects into/from string and transfer them as just opaque

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.