# Date object
- The
Date
object is a built-in object for working with dates and times - A Date object must be created through the
new Date()
constructor - The date/time is based on the browser settings, so the result depends on the timezone you're in!
# Constructor
- There are four ways to instantiate a date:
new Date()
: the current date and timenew Date(year, month, day, hours, minutes, seconds, ms)
: every property afteryear
is optionalnew Date(ms)
: milliseconds after Jan 1 1970new Date(dateString)
example | result: d = ... |
---|---|
let d = new Date(); |
|
let d = new Date(2020, 0, 31); |
|
let d = new Date(0); |
|
let d = new Date('October 16, 1964 11:13:00'); |
|
REMARK
You need to reload this page to see actual dates in the "result" column of the tables on this page!
# Frequently used methods
# Get or set portions of a date
get | set | description |
---|---|---|
getDay() (opens new window) | get the day of the week from 0 (Sunday) to 6 (Saturday) | |
getDate() (opens new window) | setDate() (opens new window) | get/set the day of the month from 1 to 31 |
getMonth() (opens new window) | setMonth() (opens new window) | get/set the month from 0 (Januari) to 11 (December) |
getFullYear() (opens new window) | setFullYear() (opens new window) | get/set the year |
getHours() (opens new window) | setHours() (opens new window) | get/set the hour from 0 to 23 |
getMinutes() (opens new window) | setMinutes() (opens new window) | get/set the minutes from 0 to 59 |
getSeconds() (opens new window) | setSeconds() (opens new window) | get/set the seconds from 0 to 59 |
getMilliseconds() (opens new window) | setMilliseconds() (opens new window) | get/set the milliseconds from 0 to 999 |
# Convert date to different formats
convert | result |
---|---|
toString() (opens new window) |
|
toLocaleString() (opens new window) | (browser default format and timezone) |
toLocaleString('en-US', { timeZone: 'America/New_York' }) |
|
toUTCString() (opens new window) |
|
toISOString() (opens new window) |
|
toDateString() (opens new window) |
|
toLocaleDateString() (opens new window) | (browser default format and timezone) |
toLocaleDateString('en-US', { timeZone: 'America/New_York' }) |
|
toTimeString() (opens new window) |
|
toLocaleTimeString() (opens new window) | (browser default format and timezone) |
toLocaleTimeString('en-US', { timeZone: 'America/New_York' }) |
|
TIP
- It's not always that easy to parse, validate, manipulate and display dates and times in the way you want
- There are several JavaScript libraries that can help you with this:
- Moment.js (opens new window): you see this library often in examples, but the project has retired on september 2020
- Luxon (opens new window): the successor of Moment.js but with a different API
- Day.js (opens new window): a fast (2 kB) replacement for Moment.js with the same API as Moment.js
# Examples
# Days before Christmas (native version)
- Open es6/date/christmasNative.html and es6/date/christmasNative.js
# Days before Christmas (with Luxon - OPTIONAL)
- What is missing in the native date functions is the retrieval of the day in text form: we only get a number for the day of the week with
getDay()
- To get the name of the day, you have to translate it yourself with, for example, an array
getDay()
is0
means Sunday,getDay()
is1
means Monday, ...
- To get the name of the day, you have to translate it yourself with, for example, an array
- The libraries mentioned in the tip above have these translations built-in, and some of them even in different languages
- This (OPTIONAL) example demonstrates how you can use Luxon to get the same (or an even better) result as in the previous example
- Open es6/date/christmasLuxon.html and es6/date/christmasLuxon.js