# 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 time
    • new Date(year, month, day, hours, minutes, seconds, ms): every property after year is optional
    • new Date(ms): milliseconds after Jan 1 1970
    • new 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:

# 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() is 0 means Sunday, getDay() is 1 means Monday, ...
  • 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
Last Updated: 4/20/2021, 2:26:56 PM