Setting a specific date

Hey ,

I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.

Please upgrade (use this link) access this content.

I'm super eager to help you learn more!

Setting a specific date

JavaScript’s Date object lets you set a specific date/time. To set a date/time, you pass a value into the Date object.

const Christmas = new Date('2019-12-25')
console.log(Christmas) // Wed Dec 25 2019 08:00:00 GMT+0800 (Singapore Standard Time)

There are three ways to get Date with a specific date/time:

  1. Pass in arguments for year, month, day, hours, minutes, seconds, and milliseconds.
  2. Pass in a date string.
  3. Pass in a Unix Timestamp value.

Passing in arguments to Date

You can pass in up to 7 arguments to create a date/time.

  1. Year: 4-digit year.
  2. Month: Month of the year (0-11). This is zero-indexed. Defaults to 0 if omitted.
  3. Day: Day of the month (1-31). Defaults to 1 if omitted.
  4. Hour: Hour of the day (0-23). Defaults to 0 if omitted.
  5. Minutes: Minutes (0-59). Defaults to 0 if omitted.
  6. Seconds: Seconds (0-59). Defaults to 0 if omitted.
  7. Milliseconds: Milliseconds (0-999). Defaults to 0 if omitted.

Here are some details to watch out for:

  1. Date defaults to the current time if no arguments are present.
  2. Date is always created in Local Time. If you want UTC time, use new Date(Date.UTC(...args)).

Here’s are some examples:

// 25th December, 2019 at 8am. Local Time.
const date = new Date(2019, 11, 25, 8)
// 25th December, 2019 at 8am. UTC
const UTCDate = new Date(Date.UTC(2019, 11, 25, 8))

Passing a date string to Date

You can pass in a date string to the Date object. I discourage this approach because different browsers accept different date formats.

If you ever need to pass in a date string, you should use the ISO 8601 Extended format.

// ISO 8601 Extended format
`YYYY-MM-DDTHH:mm:ss:sssZ`

Here’s what the values mean:

  • YYYY: 4 digit year
  • MM: 2 digit month (where January is 01 and December is 12)
  • DD: 2 digit date (01 to 31)
  • T: Indicates the start of time
  • HH: 24-digit hour (00 to 23)
  • mm: Minutes (00 to 59)
  • ss: Seconds (00 to 59)
  • sss: Milliseconds (000 to 999)
  • Z: If Z is present, date will be set to UTC. If Z is not present, it’ll be Local Time
  • -: Date delimiters
  • :: Time delimiters

Some notes:

  1. Most browsers support the ISO 8601 format.
  2. Browser support becomes wonky if you did not pass the values as expected. For example:
    1. Safari returns invalidDate if you pass in 2019-2 (single-digit-month).
    2. Chrome returns invalidDate if you passed in 2019-02T00 (Chrome requires THH:mm at least).
  3. Always watch for UTC vs Local Time. (See next section).

Examples:

// 25th December, 2019 at 8am. Local Time
const date = new Date('2019-12-25T08:00')
// 25th December, 2019 at 8am. UTC.
const date = new Date('2019-12-25T08:00Z')

Always specify the timezone offset with the date string approach

The date string approach has a unique rule. The following applies if you omit the timezone offset:

  1. date-only strings (like 2019-12-25) are created in UTC.
  2. date-time strings (like 2019-12-25T08:00) are created in Local Time.
// 25th December, 2019. UTC
const date = new Date('2019-12-25')
// 25th December, 2019. Local Time
const date = new Date('2019-12-25T00:00')

This rule is confusing because all other Date methods default to Local Time. This one rule is the exception.

So, make sure you include THH:mm if you use the date string approach. This makes date creation consistent across different methods.

Passing in a Unix timestamp

Date/time in JavaScript starts counting from 1 January, 1970, at 12am. This time is known as the Unix Epoch time. (Epoch means the beginning of a period in history).

A timestamp is the amount of time since Unix Epoch time. Timestamps can be written in seconds or milliseconds, depending on the programming language. In JavaScript, we calculate timestamps in milliseconds.

To get the timestamp, you can use getTime.

const oct2018 = new Date(2018, 9)
const timestamp = oct2018.getTime()
console.log(timestamp) // 1538323200000

Alternatively, you can use Date.now() to get the current timestamp.

const timestamp = Date.now()
console.log(timestamp) // 1548229765339

If you pass in a timestamp to the Date object, it will create a date at that timestamp.

const christmas2016 = new Date(1482624000000)
console.log(christmas2016) // Sun Dec 25 2016 08:00:00 GMT+0800 (Singapore Standard Time)

A minor detail about timestamps

Timestamps do not include leap seconds. This means you may occasionally see two times with the same timestamp.

Time UNIX Timestamp
1998-12-31T23:59:60.000Z 915148800.00
1999-01-01T00:00:00.000Z 915148800.00

This is tiny leap second thing not an issue in everyday programming. But it’s something that you should know.

Exercise

Set the following date/time:

  1. With the arguments approach:
    1. Set date to 2015-08-28 and time to 12:30pm Local Time
    2. Set date to 2015-05-20 and time to 4pm UTC
  2. With the date string approach:
    1. Set date to 2015-03-28 and time to Midnight Local Time
    2. Set date to 2015-02-20 and time to 4pm UTC
  3. With the timestamp approach:
    1. Get timestamp for 2015-04-30T04:30:33.555Z
    2. Set date to 1548449765339. What’s this date?