JavaScript date retrieval methods
- Previous Page JS Date Format
- Next Page JS Date Setting Methods
Date methods allow you to get and set date values (year, month, day, hour, minute, second, millisecond)
Date retrieval methods
The get method is used to get a part of the date (information from the date object). Below are the most commonly used methods (sorted in alphabetical order):
Method | Description |
---|---|
getDate() | Return the day as a number (1-31) |
getDay() | Get the name of the week as a number (0-6) |
getFullYear() | Get four-digit year (yyyy) |
getHours() | Get hours (0-23) |
getMilliseconds() | Get milliseconds (0-999) |
getMinutes() | Get minutes (0-59) |
getMonth() | Get month (0-11) |
getSeconds() | Get seconds (0-59) |
getTime() | Get time (from January 1, 1970 to now) |
getTime() method
getTime()
The method returns the number of milliseconds since January 1, 1970:
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getTime();
getFullYear() method
getFullYear()
The method returns the year of the date in four-digit form:
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getFullYear();
getMonth() method
getMonth()
Return the month of the date as a number (0-11):
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getMonth();
In JavaScript, the first month (January) is month number 0, so December returns month number 11.
You can use an array of names, and use getMonth()
Return the month as a name:
Example
var d = new Date(); var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ; document.getElementById("demo").innerHTML = months[d.getMonth()];
getDate() method
getDate()
The method returns the day (1-31) of the date as a number:
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getDate();
getHours() method
getHours()
The method returns the number of hours (0-23) of the date:
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getHours();
getMinutes() method
getMinutes()
The method returns the number of minutes (0-59) of the date:
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getMinutes();
getSeconds() method
getSeconds()
The method returns the number of seconds (0-59) of the date:
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getSeconds();
getMilliseconds() method
getMilliseconds()
The method returns the number of milliseconds (0-999) of the date:
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getMilliseconds();
getDay() method
getDay()
The method returns the name of the weekday (weekday) as a number (0-6):
Example
var d = new Date(); document.getElementById("demo").innerHTML = d.getDay();
In JavaScript, the first day of the week (0) represents "Sunday", even though some countries in the world consider the first day of the week to be "Monday".
You can use an array of names, and use getDay()
Returns the name of the week as the name:
Example
var d = new Date(); var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; document.getElementById("demo").innerHTML = days[d.getDay()];
UTC date method
The UTC date method is used to handle UTC dates (Universal Time Zone dates):
Method | Description |
---|---|
getUTCDate() | Is equal to getDate(), but returns UTC date |
getUTCDay() | Is equal to getDay(), but returns UTC day |
getUTCFullYear() | Is equal to getFullYear(), but returns UTC year |
getUTCHours() | Is equal to getHours(), but returns UTC hours |
getUTCMilliseconds() | Is equal to getMilliseconds(), but returns UTC milliseconds |
getUTCMinutes() | Is equal to getMinutes(), but returns UTC minutes |
getUTCMonth() | Is equal to getMonth(), but returns UTC month |
getUTCSeconds() | Is equal to getSeconds(), but returns UTC seconds |
Complete JavaScript Date Reference Manual
For a complete date reference manual, please visit our full JavaScript Date Reference Manual.
This manual includes descriptions and examples of all date properties and methods.
- Previous Page JS Date Format
- Next Page JS Date Setting Methods