How to parse date time from json?

13,914

Use this in your javascript (while jsonDate = "\/Date(1369332000000)\/"):

var date = new Date(parseInt(jsonDate.substr(6)));
var formattedDate = date.format("dd-MM-yyyy");

Source: How do I format a Microsoft JSON date?

Share:
13,914
Atish Dipongkor
Author by

Atish Dipongkor

Updated on August 21, 2022

Comments

  • Atish Dipongkor
    Atish Dipongkor over 1 year

    I have a Action Method like this

    public ActionResult TodayJson()
            {
                DateTime today = DateTime.Today;
                return Json(today,JsonRequestBehavior.AllowGet);
            }
    

    that returns me the the following value

    "\/Date(1369332000000)\/"
    

    How can I parse it in actual date time by the jquery or java script. Format should be like this dd-mm-yyyy

  • Mike Christensen
    Mike Christensen almost 11 years
    Note that solution wouldn't be time zone safe.
  • Adam Tal
    Adam Tal almost 11 years
    @MikeChristensen - stackoverflow.com/a/2316066/1203115 - look at the second comment (+35), it will work if timezone offset is specified.
  • Atish Dipongkor
    Atish Dipongkor almost 11 years
    now it gives "24-00-2013". it's not the actual value
  • Mike Christensen
    Mike Christensen almost 11 years
    Yes, however your date format is "dd-mm-yyyy".
  • Mike Christensen
    Mike Christensen almost 11 years
    @AtishDipongkor - You probably want dd-MM-yyyy, lower case mm is minute, not month.
  • Adam Tal
    Adam Tal almost 11 years
    Sorry had a mistake in the date.format string.
  • Atish Dipongkor
    Atish Dipongkor almost 11 years
    thanks @AdamFridental. It's working now