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(); 
Share:
11,076

Related videos on Youtube

andy94
Author by

andy94

Updated on June 04, 2022

Comments

  • andy94
    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?

    Code

    • Teemu
      Teemu almost 6 years
      Please add your code to the post, as text.
    • DontVoteMeDown
      DontVoteMeDown almost 6 years
      Date.now() does not returns a Date object.
    • andy94
      andy94 almost 6 years
      Just fix it: let date = new Date(Date.now()); let fixedDateFormat = date.toUTCString();
  • RobG
    RobG almost 6 years
    I can't see how new Date() is less readable than new Date()Date.now()), in which the Date.now call is entirely redundant.
  • Noah Rose Ledesma
    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
    RobG almost 6 years
    There 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, since Date() returns a human readable string that is easily understood rather than the seemingly meaningless number returned by Date.now().