IMG-LOGO

Convert DateTime to JSON DateTime format in Javascript

andy - 27 Dec, 2013 26569 Views 1 Comment

To convert the datetime to JSON format /Date(#)/ is pretty simple. You can use the following function that will accept a string of date and will convert it as a date object and return as a string json format.

Alternatively, you can use our free JSON datetime converter to help you convert the time to readable datetime.

function convertToJSONDate(strDate){
    var dt = new Date(strDate);
    var newDate = new Date(Date.UTC(dt.getFullYear(), dt.getMonth(), dt.getDate(), dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMilliseconds()));
    return '/Date(' + newDate.getTime() + ')/';
}

Example on how to use it:

convertToJSONDate("22 Jan 2013 15:00:00");

/* and this should return the following value:*/
/Date(1358866800000)/

Comments

Rajesh Rinhayat
17 Feb, 2017
This is good solution Thanks
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles