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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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());
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