.NET DateTime to JSON UNIX JavaScript datetime

When posting datetimes to web services or REST services, you might need to convert the standard .NET DateTime to a UNIX format.

Please note than neither REST, nor JavaScript has its own DateTime format. But some systems based on REST (Java applications for example) have a love affair with the UNIX Epoch datetime format, which is the number of seconds since 1/1/1970.

So in order to convert a standard .NET DateTime to a number of seconds, you need to calculate the TimeSpan between the current DateTime and 1/1/1970:

public string ToUnixEpoch(DateTime dateTime)
  DateTime d1 = new DateTime(1970, 1, 1);
  DateTime d2 = dateTime.ToUniversalTime();
  TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
  return ts.TotalMilliseconds.ToString("#");

The string returned contains the integer of the timespan.

In some systems you need to add /Date()/ around your timespan, so the resulting UNIX Epoch string looks like this:


To convert the UNIX datetime to a .NET DateTime you reverse the process:

public DateTime FromUnixEpoch(long epochTime)
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return epoch.AddSeconds(epochTime);

Some systems will return the UTC time, not the time from the current timezone. To convert from UTC to the current timezone, use the TimeZone class from .NET:



About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, General .NET and tagged , , , , . Bookmark the permalink.

One Response to .NET DateTime to JSON UNIX JavaScript datetime

  1. Hi, thanks for article.
    from my point of view but it is not full. I would like to notice that your examples is good just in case then client and time zone working in one time zone.
    in case you clients from different time zone to get correct UTC vlaue use TimeZoneInfo.ConvertTimeToUtc.
    for example:

    /// Method for converting a System.DateTime value to a UNIX Timestamp in milliseconds.
    /// Default time zone is server
    /// date to convert
    /// Milliseconds
    public static Int64 ConvertToUnixDateTimeMs(this DateTime value, TimeZoneInfo timeZoneInfo)
    timeZoneInfo = timeZoneInfo ?? TimeZoneInfo.Local;
    var date = TimeZoneInfo.ConvertTimeToUtc(value, timeZoneInfo);
    //DateTimeOffset of = new DateTimeOffset(value);
    TimeSpan span = date.Subtract(_unixStartTimeStamp.DateTime);
    //return the total seconds (which is a UNIX timestamp)
    return (long)span.TotalMilliseconds;
    also it it a lot of inserting cases to work with client time zones….


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s