Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: variable.getHours(); is undefined, where variable = Date.now()?

Am looking to get the current Datetime, and extract Hours, Minutes, etc., for adding a timestamp to messages..

Why does the console log TypeError: Date.datetimeNow is undefined for this:

var datetimeNow = Date.now();
var hourNow = datetimeNow.getHours();
var minuteNow = datetimeNow.getMinutes();
like image 611
StackThis Avatar asked May 22 '14 06:05

StackThis


3 Answers

Instead of:

var datetimeNow = Date.now();

try this:

var datetimeNow = new Date();
like image 78
Lauris Avatar answered Oct 11 '22 12:10

Lauris


var datetimeNow = new Date();
var hourNow = datetimeNow.getHours();
var minuteNow = datetimeNow.getMinutes();

You need to init datetimeNow as date object

like image 34
jhyap Avatar answered Oct 11 '22 12:10

jhyap


Date.now() returns a number representing the number of milliseconds since the UNIX epoch (1970/1/1 00:00:00.000 UTC). Numbers don't have Date methods.

To use Date methods, you need to initialize datetimeNow as a Date object.

var datetimeNow = new Date();
like image 23
Scimonster Avatar answered Oct 11 '22 12:10

Scimonster