5.13 Date
The last built-in object we look at in this section is the Date object. JavaScript has many other built-in objects — you will discover more of them as the course goes on — but Date is one you'll use almost daily. Date is a constructor function, so we create dates with the new keyword. It has several overloads.
const now = new Date(); // current date and time
const fromStr = new Date("May 15 2022 09:00"); // from a string
const exact = new Date(2022, 4, 15, 9, 0); // year, month (0-based), day, hour, min
With no argument, new Date() returns the current date and time. With a string, the engine parses common date formats (see MDN's "Date string" section for the list). With numbers, the order is year, month, day, hour, minutes, seconds, milliseconds. Watch out: months are zero-based, so 0 means January and 11 means December — use 4 for May.
Reading and formatting
date.getDate(),getFullYear(),getHours(),getMinutes(),getMilliseconds()… — read individual parts.setFullYear(2021),setHours(...), etc. — mutate the date.date.toString()— human-readable, locale-aware string like"Thu Mar 18 2021 ...".date.toTimeString()— only the time component.date.toISOString()— standard ISO format"2021-03-18T09:00:00.000Z".
The ISO format produced by toISOString() is the one you'll use the most when your web or mobile app talks to a backend server, because both sides understand it without ambiguity. That's it for this video on dates — see you in the next one.
Summary
This lesson covers JavaScript's built-in Date object, including how to create date instances using different constructors (with no parameters, string values, or individual numeric parameters). The lesson explains key concepts like zero-based month indexing (0 for January, 11 for December) and demonstrates essential Date methods for getting and setting date/time components, as well as converting dates to strings for display and communication with servers.
Key points
- Create Date objects using new Date() with no parameters (current date), a string (e.g., 'May 15 2022 9:00'), or numeric parameters for year, month (0-11), day, hours, minutes, and seconds
- Date objects use zero-based month indexing where 0 represents January and 11 represents December, which can be a common source of confusion
- Use getter methods like getDay(), getFullYear(), getHours(), getMinutes() to retrieve date/time components, and setter methods like setYear(), setHours() to modify them
- Convert dates to strings using toString(), toTimeString(), or toISOString() to display and transmit date information
- The toISOString() method returns a standardized format commonly used for client-server communication in web and mobile applications
FAQ
Why does JavaScript use 0-based indexing for months?
JavaScript's Date object represents months as 0-11 (January through December), where 0 equals January. This is a historical design choice in the language, so you must account for this when creating or comparing dates to avoid off-by-one errors.
What is the difference between toString(), toTimeString(), and toISOString()?
toString() returns the full date and time in a readable format (e.g., 'Thursday March 18 2021'), toTimeString() returns only the time component, and toISOString() returns a standardized ISO 8601 format (e.g., '2021-03-18T09:00:00Z') widely used in server communication and APIs.
Can I omit trailing parameters when creating a Date with numeric arguments?
Yes. When using the numeric constructor new Date(year, month, day, hours, minutes, seconds, milliseconds), any trailing parameters you omit will default to 0, so you can omit hours, minutes, seconds, and milliseconds if you only need to specify the date.