JS Dates
const date = new Date();
const date = new Date(year, month, day);
date.getFullYear();
date.getMonth();
Creating Date Objects
new Date() with no arguments creates a Date object representing the current moment, while passing a year, month, and day creates a Date representing that specific point in time.
Example: Creating Date Objects
// Declare the constant `now` as a new `Date` instance
// Declare the constant `now` as a new `Date` instance
const now = new Date();
// Declare the constant `specific` as a new `Date` instance
// Declare the constant `specific` as a new `Date` instance
const specific = new Date(2024, 0, 15);
// Print `now` to the console
// Print `now` to the console
console.log(now);
// Print `specific` to the console
// Print `specific` to the console
console.log(specific);
Getting Date Parts
Methods like getFullYear(), getMonth(), getDate(), and getDay() extract individual pieces of a Date object, the year, month, day of the month, and day of the week respectively.
Example: Getting Date Parts
// Declare the constant `date` as a new `Date` instance
// Declare the constant `date` as a new `Date` instance
const date = new Date(2024, 5, 20);
// Print `date.getFullYear()` to the console
// Print `date.getFullYear()` to the console
console.log(date.getFullYear());
// Print `date.getMonth()` to the console
// Print `date.getMonth()` to the console
console.log(date.getMonth());
// Print `date.getDate()` to the console
// Print `date.getDate()` to the console
console.log(date.getDate());
// Print `date.getDay()` to the console
// Print `date.getDay()` to the console
console.log(date.getDay());
Formatting Dates
toLocaleDateString() and toLocaleTimeString() format a Date object into a readable string based on locale conventions, letting the same date display naturally for visitors in different regions.
Example: Formatting Dates
// Declare the constant `date` as a new `Date` instance
// Declare the constant `date` as a new `Date` instance
const date = new Date(2024, 5, 20);
// Print `date.toLocaleDateString()` to the console
// Print `date.toLocaleDateString()` to the console
console.log(date.toLocaleDateString());
// Print `date.toLocaleTimeString()` to the console
// Print `date.toLocaleTimeString()` to the console
console.log(date.toLocaleTimeString());
Comparing Dates
Date objects can be compared using relational operators like < and >, which JavaScript converts to timestamps automatically, though for equality checks it's more reliable to explicitly compare their getTime() values.
Example: Comparing Dates
// Declare the constant `d1` as a new `Date` instance
// Declare the constant `d1` as a new `Date` instance
const d1 = new Date(2024, 0, 1);
// Declare the constant `d2` as a new `Date` instance
// Declare the constant `d2` as a new `Date` instance
const d2 = new Date(2024, 5, 1);
// Print `d1 < d2` to the console
// Print `d1 < d2` to the console
console.log(d1 < d2);
// Print `d1.getTime() === d2.getTime()` to the console
// Print `d1.getTime() === d2.getTime()` to the console
console.log(d1.getTime() === d2.getTime());
Timestamps
A timestamp is the number of milliseconds elapsed since January 1, 1970 (the Unix epoch), obtained from Date.now() for the current moment or getTime() on an existing Date object, and it's a universal, timezone-independent way to represent a moment.
Example: Timestamps
// Print `Date.now()` to the console
// Print `Date.now()` to the console
console.log(Date.now());
// Declare the constant `date` as a new `Date` instance
// Declare the constant `date` as a new `Date` instance
const date = new Date();
// Print `date.getTime()` to the console
// Print `date.getTime()` to the console
console.log(date.getTime());
- Forgetting that getMonth() returns a zero-based value, January is 0, not 1, a very common source of off-by-one bugs.
- Assuming Date objects automatically format nicely for display, they require explicit formatting methods to look presentable.
- Comparing dates with == or === directly, which compares object references rather than the actual moments in time they represent.
- The Date object represents a specific moment in time and can be created for the current moment or a specific date.
- Methods like getFullYear, getMonth, and getDate extract individual pieces of a date.
- Dates can be compared by converting them to timestamps with getTime(), and formatted for display with methods like toLocaleDateString.
The Date object and its core methods are supported identically in every browser, though locale-based formatting output can vary slightly by system settings.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic