How do I parse "/Date(1459934700000)/" into a JavaScript Date object?

I'm trying to parse a date expressed as "/Date(1459934700000)/" via jQuery. I use this code :

new Date(parseInt(myDateString.substr(6)))

and get me this date :

Sun Apr 26 1970 02:58:20 GMT+0430 (Iran Standard Time)

But I want to get date such as 4/6/2016 12:45:00 PM.

Answers 1

  • You can use to create a custom function of your own like:

    var dt = "/Date(1459934700000)/"; // <---- actual date string 
    dt = parseInt(dt.slice(6, -2), 10); // extract the date in ms and parsing it back to numbers
    
    
    function dateConverter(d, sep){
      var d = new Date(d),   // <-------converting it back to a valid date here
          date = d.getDate(),
          month = d.getMonth()+1,
          year = d.getFullYear(),
          hrs = d.getHours(),
          min = d.getMinutes(),
          sec = d.getSeconds(),
          ampm = hrs >= 12 ? " PM" : " AM";
      
      return month+sep+date+sep+year+" "+hrs+":"+min+":"+sec+ampm;
      
    }
    
    document.querySelector('pre').innerHTML = dateConverter(dt, '/'); // <---passed the date in ms here
    <pre></pre>


Related Articles