Date.now().toISOString() throwing error "not a function"

node.js

node.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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrmcneillyView Question on Stackoverflow
Solution 1 - node.jsAdrian TheodorescuView Answer on Stackoverflow
Solution 2 - node.jsmeAndrewView Answer on Stackoverflow