rmcneilly rmcneilly - 1 year ago 166
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 Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download