JS Dates
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.
Note: When building a specific date, remember the month argument is zero-based, so January is 0 and December is 11.
Warning: new Date('some string') can behave inconsistently across different browsers for ambiguous formats, prefer numeric arguments for reliability.
Example: Creating Date Objects
const now = new Date();
const specific = new Date(2024, 0, 15);
console.log(now);
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.
Note: getDay() returns the day of the week as a number, 0 for Sunday through 6 for Saturday, useful for custom weekday labels.
Warning: getMonth() returns 0 through 11, always add 1 if you want to display a human-friendly month number.
Example: Getting Date Parts
const date = new Date(2024, 5, 20);
console.log(date.getFullYear());
console.log(date.getMonth());
console.log(date.getDate());
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.
Note: Passing a locale code, like en-GB or de-DE, to toLocaleDateString() lets you format a date according to a specific regional convention.
Warning: Without specifying a locale, formatting output depends on the visitor's browser and system settings, which can vary between users.
Example: Formatting Dates
const date = new Date(2024, 5, 20);
console.log(date.toLocaleDateString());
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.
Note: getTime() returns the number of milliseconds since January 1, 1970, a universal reference point that makes precise comparison straightforward.
Warning: Using === to compare two different Date objects representing the same moment returns false, since they're different object instances, not equal values.
Example: Comparing Dates
const d1 = new Date(2024, 0, 1);
const d2 = new Date(2024, 5, 1);
console.log(d1 < d2);
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.
Note: Timestamps are ideal for storing dates in a database or comparing durations, since they're simple numbers rather than complex objects.
Warning: Date.now() returns a number, not a Date object, calling Date methods directly on its result will fail without wrapping it in new Date() first.
Example: Timestamps
console.log(Date.now());
const date = new Date();
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