0

I want to convert the JavaScript date object

let date = Date.now();

to date in RFC 3339 format, so it would return like this:

2020-05-21T08:01:48+00:00

How can I convert it? Get Help! Thank you!~

ImStuck
  • 13
  • 4
  • You might try `new Date().toISOString().replace(/\.\d{3}Z/, '+00:00')`. BTW, `new Date().toISOString()` is RFC 3339 compliant. – RobG May 21 '20 at 09:26
  • i want to convert current date, which means i must use Date.now() or just use the code for current date? – ImStuck May 21 '20 at 09:28
  • and btw, i want the date to minus by 1 week, that is why i use Date.now(), how can i use yours with the subtraction? – ImStuck May 21 '20 at 09:30
  • [Date.now()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now) returns a time value for the current instant, [new Date()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date) returns a date instance with exactly the same time value (i.e. you don't need to use Date.now). For subtracting a week, see [*How can I add 1 day to current date?*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date) Just subtract 7 instead of adding 1. – RobG May 21 '20 at 09:50
  • Please try the search field at the top of the page, it's very useful. Prefix terms with \[javascript\] to restrict searches to javascript. – RobG May 21 '20 at 09:52

1 Answers1

0

Try toISOString.

new Date('2020-05-21 08:01:48').toISOString()
slashmelon
  • 162
  • 14