Loading lesson path
Concept visual
Start at both ends
In JavaScript, date objects are created with new Date(). new Date() returns a date object with the current date and time.
Get the Current Time const date = new Date();Get year as a four digit number (yyyy) getMonth()
Formula
Get month as a number (0 - 11)getDate()
Formula
Get day as a number (1 - 31)getDay()
Formula
Get weekday as a number (0 - 6)getHours()
Formula
(0 - 23)getMinutes()
Formula
(0 - 59)getSeconds()
Formula
(0 - 59)getMilliseconds()
Formula
(0 - 999)getTime()
(milliseconds since January 1, 1970)
Local time.
(UTC) is documented at the bottom of this page.
The get methods return information from existing date objects.
In a date object, the time is static. The "clock" is not "running".The time in a date object is NOT the same as current time.
method returns the year of a date as a four digit number:
Examples const d = new Date("2021-03-25");
d.getFullYear();
const d = new Date();
d.getFullYear();Warning !
Formula
Old JavaScript code might use the non - standard method getYear().getYear() is supposed to return a 2-digit year.
getYear() is deprecated. Do not use it!Formula
method returns the month of a date as a number (0 - 11).In JavaScript, January is month number 0, February is number 1, ... Finally, December is month number 11.
Examples const d = new Date("2021-03-25");
d.getMonth();
const d = new Date();
d.getMonth();You can use an array of names to return the month as a name:
Examples const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];Formula
method returns the day of a date as a number (1 - 31):Examples const d = new Date("2021-03-25");
d.getDate();
const d = new Date();
d.getDate();Formula
method returns the hours of a date as a number (0 - 23):Examples const d = new Date("2021-03-25");
d.getHours();
const d = new Date();
d.getHours();Formula
method returns the minutes of a date as a number (0 - 59):Examples const d = new Date("2021-03-25");
d.getMinutes();
const d = new Date();
d.getMinutes();Formula
method returns the seconds of a date as a number (0 - 59):Examples const d = new Date("2021-03-25");
d.getSeconds();
const d = new Date();
d.getSeconds();