admin管理员组

文章数量:1332345

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);

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);
Share Improve this question edited May 10, 2018 at 16:42 John Slegers 47.1k23 gold badges204 silver badges173 bronze badges asked May 8, 2018 at 7:54 Mohammad IrshadMohammad Irshad 1391 gold badge3 silver badges11 bronze badges 2
  • Can you be a bit more specific? Are you trying to convert '9999-12-31T00:00:00Z' to a timestamp? – Ahsan Commented May 8, 2018 at 8:00
  • var foo = '/Date(' + (new Date('9999-12-31T00:00:00Z')).getTime() + ')/' – Salman Arshad Commented May 8, 2018 at 8:17
Add a ment  | 

3 Answers 3

Reset to default 4

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()

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>

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"));

本文标签: typescriptHow to convert an ISO 8601 date to 39Date(1525687010053)39 format in javascriptStack Overflow