Date. Now(). toISOString() Throwing Error "Not a Function"

Date. Now(). toISOString() Throwing Error "Not a Function"

I am running Node v6.4.0 on Windows 10. In one of my Javascript files I am trying to get an ISO date string from the Date object:

let timestamp = Date.now().toISOString();

This throws: Date.now(...).toISOString is not a function

Looking through stackoverflow this should work...possible bug in Node?

2 Answers

Date.now() returns a number which represents the number of milliseconds elapsed since the UNIX epoch. The toISOString method cannot be called on a number, but only on a Date object, like this:

var now = new Date();
var isoString = now.toISOString();

Or in one single line:

new Date().toISOString()
5

If anybody wonder if you can convert excisting date.Now() timestamp to an actual date: yes, you can. Just:

new Date(put your timestamp here).toISOString().slice(0, 10)

and you will get date in yyyy-mm-dd format.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.