Loading lessons...
Date Formats and Set
Date Formats and Set
You can read and change the parts of a date.
Date input formats
const d = new Date("2023-03-14"); // ISO format
const d2 = new Date("March 14, 2023"); // long format
Set date parts
const d = new Date();
d.setFullYear(2024);
d.setMonth(5); // June (month 5)
d.setDate(20);
d.setHours(10);
d.setMinutes(30);
d.setSeconds(0);
Full date construction
const d = new Date(2023, 2, 14, 9, 30, 0);
// March 14, 2023, 09:30:00
Set works in place
set methods modify the existing date object, so you do not assign the result.
Display formats
d.toISOString();
d.toLocaleDateString();
d.toLocaleTimeString();
TL;DR
- Dates can be created from ISO or long strings.
- setFullYear, setMonth, setDate change parts.
- get/set methods come in matching pairs.
- toLocaleDateString formats for the user.