Date.now().toISOString() throwing error "not a function"
node.jsnode.js Problem Overview
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?
node.js Solutions
Solution 1 - node.js
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()
Solution 2 - node.js
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.