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
2
: 12797 At:- 9/25/2017 12:38:26 PM
json-date javascript convert-json-date-into-valid-date convert json date to datetime







3 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());
    }
3
At:- 9/25/2017 3:49:48 PM Updated at:- 12/24/2022 6:10:17 AM
It worked for me 0
By : neena - at :- 11/1/2017 12:13:13 PM


profileImage Answered by:- vikas_jk

Client Side solution

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(', '')));
}

Server Side solution

Easy and simple solution, is to convert your DateTime into string before returning JSON

var StringDatetim = DateObject.ToString("dd/MM/yyyy");

OR

You can create a custom JSONResult class and override the ExecuteResult method, which will convert your results before sending it back to client side

Here is the Sample Class, which uses JSON.NET to serialize.

public class CustomJsonResult : JsonResult
{
    private const string _dateFormat = "dd/MM/yyyy hh:mm:ss";

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        HttpResponseBase response = context.HttpContext.Response;

        if (!String.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }
        if (Data != null)
        {
            // Using Json.NET serializer
            var isoConvert = new IsoDateTimeConverter();
            isoConvert.DateTimeFormat = _dateFormat;
            response.Write(JsonConvert.SerializeObject(Data, isoConvert));
        }
    }
}

and you can use it as below

public ActionResult Index() {
    return new CustomJsonResult { Data = new { DataList=db.TableName.ToList(); } };
}
2
At:- 10/5/2017 8:53:49 AM


profileImage Answered by:- jon

You can also try below javascript code to get date from Microsoft JSON date string 

var myDate = "/Date(1224043200000)/";

var value = new Date
            (
                 parseInt(myDate.replace(/(^.*\()|([+-].*$)/g, ''))
            );
var dat = value.getMonth() +
                         1 +
                       "/" +
           value.getDate() +
                       "/" +
       value.getFullYear();

console.log(dat);

Output:

"10/15/2008"

Thanks

0
At:- 12/24/2022 12:43:58 PM






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