rmcneilly rmcneilly - 3 months ago 28
Node.js Question

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?

Answer

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