How to format JSON date into valid date using javascript or jquery?


I am getting JSON result from server, with date as given below

/Date(1224043200000)/

now I want to convert it into valid date format like dd/MM/yyyy (25/09/2017), so how can I convert JSON date into valid date format using javascript or jquery?


Asked by:- neena
1
: 125 At:- 9/25/2017 12:38:26 PM
json-date javascript jQuery convert-json-date-into-valid-date






2 Answers
profileImage Answered by:- Sam

With the help of javascript create a function like below and pass the above JSON date  ( /Date(1224043200000)/ ) to convert it into mm/DD/yyyy

 function JavascriptDate(value) { 
        var pattern = /Date\(([^)]+)\)/;
        var results = pattern.exec(value);
        var dt = new Date(parseFloat(results[1]));
       // get date in dd/MM/yyyy format
        return (dt.getDate()+ "/"+ dt.getMonth() + 1) + "/" + dt.getFullYear());
    }
1
At:- 9/25/2017 3:49:48 PM


profileImage Answered by:- vikas_jk

You can use Regex to convert JSON date into string using the javascript code below

var dateRegex = /^\/Date\((d|-|.*)\)[\/|\\]$/;

function convertWCFStringDate(strDate) {
    var matched = dateRegex.exec(strDate);
    if (matched) {
        var parts = matched[1].split(/[-+,.]/);
        return new Date(parts[0] ? +parts[0] : 0 - +parts[1]);
    }
}

Or Another Solution can be simply using this code

function parseJsonDate(jsonDateString){
    return new Date(parseInt(jsonDateString.replace('/Date(', '')));
}
0
At:- 10/5/2017 8:53:49 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly