[ACCEPTED]-Converting .NET DateTime to JSON-datetime

Accepted answer
Score: 248

What is returned is milliseconds since epoch. You 6 could do:

var d = new Date();

On how to format the date exactly 5 as you want, see full Date reference at http://www.w3schools.com/jsref/jsref_obj_date.asp

You 4 could strip the non-digits by either parsing 3 the integer (as suggested here):

var date = new Date(parseInt(jsonDate.substr(6)));

Or applying the following 2 regular expression (from Tominator in the 1 comments):

var jsonDate = jqueryCall();  // returns "/Date(1245398693390)/"; 
var re = /-?\d+/; 
var m = re.exec(jsonDate); 
var d = new Date(parseInt(m[0]));
Score: 79

I have been using this method for a while:

using System;

public static class ExtensionMethods {
  // returns the number of milliseconds since Jan 1, 1970 (useful for converting C# dates to JS dates)
  public static double UnixTicks(this DateTime dt)
    DateTime d1 = new DateTime(1970, 1, 1);
    DateTime d2 = dt.ToUniversalTime();
    TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
    return ts.TotalMilliseconds;

Assuming 7 you are developing against .NET 3.5, it's 6 a straight copy/paste. You can otherwise 5 port it.

You can encapsulate this in a JSON 4 object, or simply write it to the response 3 stream.

On the Javascript/JSON side, you 2 convert this to a date by simply passing 1 the ticks into a new Date object:

  success: function(msg) {
    var d = new Date(msg);
Score: 34

To parse the date string using String.replace 1 with backreference:

var milli = "/Date(1245398693390)/".replace(/\/Date\((-?\d+)\)\//, '$1');
var d = new Date(parseInt(milli));
Score: 19

If you pass a DateTime from a .Net code to a javascript 2 code, C#:

DateTime net_datetime = DateTime.Now;

javascript treats it as a string, like 1 "/Date(1245398693390)/":

You can convert it as fllowing:

// convert the string to date correctly
var d = eval(net_datetime.slice(1, -1))


// convert the string to date correctly
var d = eval("/Date(1245398693390)/".slice(1, -1))
Score: 17

If you're having trouble getting to the 2 time information, you can try something 1 like this:

    d.date = d.date.replace('/Date(', '');
    d.date = d.date.replace(')/', '');  
    var expDate = new Date(parseInt(d.date));
Score: 12

the conversion from 1970,1,1 needs the double 2 rounded to zero decimal places i thinks

DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = dt.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return Math.Round( ts.TotalMilliseconds,0);

on 1 the client side i use

new Date(+data.replace(/\D/g, ''));
Score: 8


var date = eval(data.Data.Entity.Slrq.replace(/\/Date\((\d )\)\//gi, "new Date($1)"));  
alert(date.format("yyyy-MM-dd HH:mm:ss"));  
alert(dateFormat(date, "yyyy-MM-dd HH:mm:ss"));  


Score: 7

You can try a 3rd party library like json.net There's 5 documention on the project site. It does 4 say it requires .net 3.5.

Otherwise there's 3 another one called Nii.json which i believe 2 is a port from java. I found a link to 1 it on this blog

Score: 5

The previous answers all state that you 4 can do the following:

var d = eval(net_datetime.slice(1, -1));

However, this doesn't 3 work in either Chrome or FF because what's 2 getting evaluated literally is:

// returns the current timestamp instead of the specified epoch timestamp
var d = Date([epoch timestamp]);

The correct 1 way to do this is:

var d = eval("new " + net_datetime.slice(1, -1)); // which parses to

var d = new Date([epoch timestamp]); 
Score: 4

Thought i'd add my solution that i've been 6 using.

If you're using the System.Web.Script.Serialization.JavaScriptSerializer() then the time 5 returned isn't going to be specific to your 4 timezone. To fix this you'll also want to 3 use dte.getTimezoneOffset() to get it back to your correct time.

String.prototype.toDateFromAspNet = function() {
    var dte = eval("new " + this.replace(/\//g, '') + ";");
    dte.setMinutes(dte.getMinutes() - dte.getTimezoneOffset());
    return dte;

now 2 you'll just call


Fri Jun 19 2009 00:04:53 1 GMT-0400 (Eastern Daylight Time) {}

More Related questions