-2

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
hmahdavi
  • 2,250
  • 3
  • 38
  • 90

2 Answers2

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>
Jai
  • 74,255
  • 12
  • 74
  • 103
1

var testDate = 'Sun Apr 26 1970 02:58:20 GMT+0430 (Iran Standard Time)';
alert(moment(testDate).format('DD/MM/YYYY hh:mm:ss A')); 
<script src="http://momentjs.com/downloads/moment.min.js"></script>
Nic
  • 217
  • 2
  • 16