JS तारीखें
In this page:
const date = new Date();
const date = new Date(year, month, day);
date.getFullYear();
date.getMonth();
Date Objects बनाना
बिना किसी argument के new Date() एक ऐसा Date object बनाता है जो current moment को represent करता है, जबकि year, month, और day पास करने पर वह उस खास समय को represent करने वाला Date बनाता है।
उदाहरण: 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);
Date के हिस्से निकालना
getFullYear(), getMonth(), getDate(), और getDay() जैसे methods Date object के individual हिस्से निकालते हैं, क्रमशः year, month, month का day, और week का day।
उदाहरण: 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());
Dates को Format करना
toLocaleDateString() और toLocaleTimeString() locale conventions के आधार पर किसी Date object को एक readable स्ट्रिंग में format करते हैं, जिससे एक ही date अलग-अलग regions के visitors के लिए स्वाभाविक रूप से दिखती है।
उदाहरण: 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());
Dates की तुलना करना
Date objects की तुलना < और > जैसे relational operators से की जा सकती है, जिन्हें JavaScript अपने आप timestamps में बदल देता है, हालांकि equality checks के लिए उनके getTime() values को explicitly compare करना ज़्यादा भरोसेमंद है।
उदाहरण: 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
Timestamp January 1, 1970 (Unix epoch) से बीते milliseconds की संख्या है, जो current moment के लिए Date.now() से या किसी मौजूदा Date object पर getTime() से मिलती है, और यह किसी moment को represent करने का एक universal, timezone-independent तरीका है।
उदाहरण: 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());
- यह भूल जाना कि getMonth() zero-based value return करता है, January 0 है, 1 नहीं, off-by-one bugs का एक बहुत common source।
- यह मान लेना कि Date objects display के लिए automatically अच्छे से format हो जाते हैं, उन्हें presentable दिखने के लिए explicit formatting methods चाहिए।
- dates को सीधे == या === से compare करना, जो actual moments in time के बजाय object references compare करता है।
- Date object समय के एक specific moment को represent करता है और इसे current moment या किसी specific date के लिए बनाया जा सकता है।
- getFullYear, getMonth, और getDate जैसे methods किसी date के individual pieces निकालते हैं।
- Dates को getTime() से timestamps में convert करके compare किया जा सकता है, और toLocaleDateString जैसे methods से display के लिए format किया जा सकता है।
Date object और उसके core methods हर browser में समान रूप से supported हैं, हालांकि locale-based formatting output 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