Mohammad Irshad
Mohammad Irshad

Reputation: 139

How to convert an ISO 8601 date to '/Date(1525687010053)/' format in javascript?

How can I convert a date value formatted as 9999-12-31T00:00:00Z to /Date(1525687010053)/ format in javascript?

I have this, but it doesn't work:

var datevalue = '9999-12-31T00:00:00Z';
var converteddate = Date.parseDate(datevalue);

Upvotes: 1

Views: 10868

Answers (3)

John Slegers
John Slegers

Reputation: 47101

You can do your conversion in just three easy steps :

  1. Convert your ISO 8601 string to a Date object
  2. Use getTime to convert your Date object to a universal time timestamp
  3. Wrap "/Date(" and ")/" around your result

Demo

function convert(iso8601string) {
  return "/Date(" + (new Date(iso8601string)).getTime() + ")/";
}

console.log(convert("2011-10-05T14:48:00.000Z"));

Upvotes: 0

Mamadou
Mamadou

Reputation: 79

I assume that you want to get the timestamp of that date. This can be achieved with the code below

var timestamp = new Date('9999-12-31T00:00:00Z').getTime()

Upvotes: 4

hoangkianh
hoangkianh

Reputation: 649

I don't understand your question, but your code is wrong. There is no Date.parseDate() function in javascript, only Date.parse():

var datevalue = '9999-12-31T00:00:00Z'; 
var converteddate = Date.parse(datevalue);

document.getElementById('result').innerHTML = converteddate;
console.log(converteddate)
<p id="result"></p>

Upvotes: 1

Related Questions