date.toUTCString() it's not a function
11,076
As DontVoteMeDown said: Date.now() does not return a date object, instead Date.now() returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
To get the current timestamp as a Date object
var date = new Date( Date.now() );
Or more simply (but less readable)
var date = new Date();
Related videos on Youtube
Author by
andy94
Updated on June 04, 2022Comments
-
andy94 almost 2 years
Hello when i try to convert my date to format UTC it throwing TypeError: date.toUTCString it's not a function any suggestions?
-
Teemu almost 6 yearsPlease add your code to the post, as text.
-
DontVoteMeDown almost 6 years
Date.now()
does not returns aDate
object. -
andy94 almost 6 yearsJust fix it: let date = new Date(Date.now()); let fixedDateFormat = date.toUTCString();
-
-
RobG almost 6 yearsI can't see how
new Date()
is less readable thannew Date()Date.now())
, in which the Date.now call is entirely redundant. -
Noah Rose Ledesma almost 6 years@RobG there's no way to know that
new Date()
constructs a date object for the current time without having read the documentation, or guessing. New Date could construct an empty Date object -
RobG almost 6 yearsThere are many ways to obtain knowledge. The same logic could be applied to not knowing what Date.now() produces. You might even say
new Date(Date())
is more readable, sinceDate()
returns a human readable string that is easily understood rather than the seemingly meaningless number returned byDate.now()
.