Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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?

like image 689
rmcneilly Avatar asked Aug 30 '16 00:08

rmcneilly


People also ask

How do you convert toISOString to dates?

Use the Date() constructor to convert an ISO string to a date object, e.g. new Date('2023-07-21T09:35:31.820Z') . The Date() constructor will easily parse the ISO 8601 string and will return a Date object.

What is toISOString in Javascript?

toISOString() The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively).

How do you use toISOString in Javascript?

toISOString() method is used to convert the given date object's contents into a string in ISO format (ISO 8601) i.e, in the form of (YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ). The date object is created using date() constructor.


1 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() 
like image 134
Adrian Theodorescu Avatar answered Sep 21 '22 18:09

Adrian Theodorescu