← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 1 of 26

JS तारीखें

किसी airport में लगे world clock display के बारे में सोचें, जो दुनिया भर के शहरों का current time दिखाता है, हर एक अपने timezone के हिसाब से सही, पर सब एक ही underlying moment से track होते हुए। JavaScript का Date object आपके कोड के लिए वही internal clock है, जो आपको current moment capture करने, कोई खास date बनाने, और display या calculation के लिए year, month, या day जैसे हिस्से निकालने देता है। cookiescursor.com पर आने वाले international audience के लिए dates मायने रखती हैं, कोई tutorial कब publish हुआ यह दिखाना या कोई आखिरी बार कब login हुआ था यह calculate करना, दोनों ही आम, practical इस्तेमाल हैं।
Syntax
javascript
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 बनाता है।

Note: कोई खास date बनाते समय याद रखें कि month argument zero-based होता है, तो January 0 होता है और December 11।
Warning: new Date('some string') अलग-अलग browsers में ambiguous formats के लिए असंगत व्यवहार कर सकता है, भरोसेमंद रहने के लिए numeric arguments को prefer करें।

उदाहरण: Creating Date Objects

javascript
// 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।

Note: getDay() week के day को एक number के रूप में return करता है, Sunday के लिए 0 से लेकर Saturday के लिए 6 तक, जो custom weekday labels के लिए उपयोगी है।
Warning: getMonth() 0 से 11 तक return करता है, अगर आप human-friendly month number दिखाना चाहते हैं तो हमेशा 1 जोड़ें।

उदाहरण: Getting Date Parts

javascript
// 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 के लिए स्वाभाविक रूप से दिखती है।

Note: toLocaleDateString() को en-GB या de-DE जैसा एक locale code पास करने से आप किसी खास regional convention के हिसाब से date format कर सकते हैं।
Warning: कोई locale बताए बिना, formatting output visitor की browser और system settings पर निर्भर करता है, जो अलग-अलग users में अलग हो सकता है।

उदाहरण: Formatting Dates

javascript
// 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 करना ज़्यादा भरोसेमंद है।

Note: getTime() January 1, 1970 से लेकर अब तक के milliseconds की संख्या return करता है, एक universal reference point जो precise comparison को आसान बनाता है।
Warning: === से एक ही moment को represent करने वाले दो अलग-अलग Date objects की तुलना करने पर false मिलता है, क्योंकि वे अलग-अलग object instances हैं, बराबर values नहीं।

उदाहरण: Comparing Dates

javascript
// 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 तरीका है।

Note: Timestamps database में dates store करने या durations compare करने के लिए आदर्श होते हैं, क्योंकि वे complex objects के बजाय साधारण numbers होते हैं।
Warning: Date.now() एक number return करता है, Date object नहीं, उसके result पर सीधे Date methods कॉल करना बिना पहले new Date() में wrap किए fail हो जाएगा।

उदाहरण: Timestamps

javascript
// 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());
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि getMonth() zero-based value return करता है, January 0 है, 1 नहीं, off-by-one bugs का एक बहुत common source।
  2. यह मान लेना कि Date objects display के लिए automatically अच्छे से format हो जाते हैं, उन्हें presentable दिखने के लिए explicit formatting methods चाहिए।
  3. 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 के हिसाब से थोड़ा अलग हो सकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.