# Date object
- The Dateobject 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 time
- new Date(year, month, day, hours, minutes, seconds, ms): every property after- yearis optional
- new Date(ms): milliseconds after Jan 1 1970
- new Date(dateString)
 
| example | result: d = ... | 
|---|---|
| let d = new Date(); | Fri Oct 31 2025 21:57:19 GMT+0000 (Coordinated Universal Time) | 
| let d = new Date(2020, 0, 31); | Fri Jan 31 2020 00:00:00 GMT+0000 (Coordinated Universal Time) | 
| let d = new Date(0); | Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time) | 
| let d = new Date('October 16, 1964 11:13:00'); | Fri Oct 16 1964 11:13:00 GMT+0000 (Coordinated Universal Time) | 
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) to6(Saturday) | |
| getDate()(opens new window) | setDate()(opens new window) | get/set the day of the month from 1to31 | 
| getMonth()(opens new window) | setMonth()(opens new window) | get/set the month from 0(Januari) to11(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 0to23 | 
| getMinutes()(opens new window) | setMinutes()(opens new window) | get/set the minutes from 0to59 | 
| getSeconds()(opens new window) | setSeconds()(opens new window) | get/set the seconds from 0to59 | 
| getMilliseconds()(opens new window) | setMilliseconds()(opens new window) | get/set the milliseconds from 0to999 | 
# Convert date to different formats
| convert | result | 
|---|---|
| toString()(opens new window) | Fri Oct 31 2025 21:57:19 GMT+0000 (Coordinated Universal Time) | 
| toLocaleString()(opens new window) | 10/31/2025, 9:57:19 PM(browser default format and timezone) | 
| toLocaleString('en-US', { timeZone: 'America/New_York' }) | 10/31/2025, 5:57:19 PM | 
| toUTCString()(opens new window) | Fri, 31 Oct 2025 21:57:19 GMT | 
| toISOString()(opens new window) | 2025-10-31T21:57:19.991Z | 
| toDateString()(opens new window) | Fri Oct 31 2025 | 
| toLocaleDateString()(opens new window) | 10/31/2025(browser default format and timezone) | 
| toLocaleDateString('en-US', { timeZone: 'America/New_York' }) | 10/31/2025 | 
| toTimeString()(opens new window) | 21:57:19 GMT+0000 (Coordinated Universal Time) | 
| toLocaleTimeString()(opens new window) | 9:57:19 PM(browser default format and timezone) | 
| toLocaleTimeString('en-US', { timeZone: 'America/New_York' }) | 5:57:19 PM | 
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
HTML
JavaScript
result
<div class="border-green"></div>Copied!
1
# 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()is- 0means Sunday,- getDay()is- 1means 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
HTML
JavaScript
result
- Link to the Luxon CDN BEFORE you write your script
<main class="container"> ... <div class="border-green"></div> ... </main> ... <script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.26.0/luxon.min.js"></script> <script src="christmasLuxon.js"></script> ...Copied!
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
 
 
