Lesson 47 +10 XP

Date Basics

Date Basics

The built-in Date object handles dates and times.

Create a date

const now = new Date(); // current date and time

Create a specific date

const d1 = new Date("2023-03-14");
const d2 = new Date(2023, 2, 14); // year, month, day

Note the month quirk

Months are numbered from 0 (January) to 11 (December). So month 2 is March:

new Date(2023, 2, 14); // March 14, 2023

Get the parts

const d = new Date();
d.getFullYear(); // e.g. 2026
d.getMonth();    // 0-11
d.getDate();     // day of month
d.getDay();      // day of week (0 = Sunday)
d.getHours();    // 0-23
d.getMinutes();
d.getSeconds();

Display a date

d.toString();
d.toDateString();

TL;DR

  • new Date() gives the current time.
  • Create specific dates with strings or arguments.
  • Months run 0 to 11.
  • getFullYear, getMonth, getDate read parts.