JavaScript - Date get methods
In this article, we would like to describe methods thet can be used to get information from Date
object in JavaScript.
Method | Description |
---|---|
getDate() | Gets the day number in the month (from 1 to 31 depemening on month) |
getMonth() |
Gets the month index number (from
|
getFullYear() | Gets the year number (format: YYYY ) |
getHours() | Gets the hours (from 0 to 23 ) |
getMinutes() | Gets the minutes (from 0 to 59 ) |
getSeconds() | Gets the seconds (from 0 to 59 ) |
getMilliseconds() | Gets the milliseconds (fomr 0 to 999 ) |
getDay() |
Gets the weekday index number (from
|
getTime() | Gets the time in milliseconds since Unix Epoch (January 1, 1970) |
Static Method | Description |
---|---|
Date.now() |
Gets the current time in milliseconds since Unix Epoch (January 1, 1970)
|
Note:
The
Date.now()
is equivalent togetTime()
, however you should useDate.now()
since it is faster.xxxxxxxxxx
1const now1 = Date.now();
2const now2 = new Date().getTime();
In this section, we present mentioned methods.
xxxxxxxxxx
const date = new Date('2021-09-07T15:22:11.000Z');
const day = date.getDate();
const month = date.getMonth();
const year = date.getFullYear();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const milliseconds = date.getMilliseconds();
const weekDayIndex = date.getDay();
console.log(day); // 7
console.log(month); // 8
console.log(year); // 2021
console.log(hours); // 17
console.log(minutes); // 22
console.log(seconds); // 11
console.log(milliseconds); // 0
console.log(weekDayIndex); // 2
Note:
The
hours
in ISO format may be different depending on the time zone you live in. My time is currently given inGMT+0200 (Central European Summer Time)
, that's why ISO format adds 2 hours (from15
to17
).
Note:
To see the difference between
Date.now()
andgetTime()
go to the article from See also section.