JavaScript Dates
JavaScript में Date object का उपयोग तारीख (date) और समय (time) से जुड़ी जानकारी को बनाने, पढ़ने, और बदलने के लिए किया जाता है।Date
का उपयोग calendars, clocks, timers, logs, और real-time data जैसी कई चीज़ों के लिए किया जाता है।
इस अध्याय में आप सीखेंगे:
- JavaScript में date कैसे बनाई जाती है
- Dates से information कैसे निकाली जाती है (जैसे year, month, day)
- Date formats और methods
- Examples और अभ्यास प्रश्न
🔹 1. Date Object कैसे Create करें?
✅ Current Date and Time
const now = new Date();
console.log(now);
// Example Output: Wed Jun 26 2025 16:23:10 GMT+0530 (India Standard Time)
✅ Specific Date (Year, Month, Day)
const birthday = new Date(2020, 4, 15); // Months start from 0 (i.e. May = 4)
console.log(birthday); // Output: Fri May 15 2020
✅ Date as String
const someDate = new Date("2023-12-25");
console.log(someDate); // Output: Mon Dec 25 2023
⚠️ Month हमेशा 0 से शुरू होता है:
- January = 0
- December = 11
🔹 2. Useful Date Methods
🔸 getFullYear()
const d = new Date();
console.log(d.getFullYear()); // e.g., 2025
🔸 getMonth()
console.log(d.getMonth()); // 0 = January, 11 = December
🔸 getDate()
Month का दिन (1–31)
console.log(d.getDate()); // e.g., 26
🔸 getDay()
सप्ताह का दिन (0 = Sunday, 6 = Saturday)
console.log(d.getDay()); // e.g., 4 (Thursday)
🔸 getHours()
, getMinutes()
, getSeconds()
, getMilliseconds()
console.log(d.getHours()); // e.g., 16
console.log(d.getMinutes()); // e.g., 23
console.log(d.getSeconds()); // e.g., 10
🔹 3. Set Methods – तारीख को Modify करना
const d = new Date();
d.setFullYear(2030);
d.setMonth(0); // January
d.setDate(1);
console.log(d); // Output: Tue Jan 01 2030
🔹 4. toDateString()
, toISOString()
, toLocaleDateString()
const d = new Date();
console.log(d.toDateString()); // "Wed Jun 26 2025"
console.log(d.toISOString()); // "2025-06-26T10:53:00.000Z"
console.log(d.toLocaleDateString()); // "26/6/2025" (India Locale)
🔹 5. Timestamp: getTime()
Milliseconds since Jan 1, 1970 (Unix Epoch)
const d = new Date();
console.log(d.getTime()); // e.g., 1756236000000
🔹 6. Comparing Dates
let d1 = new Date("2024-01-01");
let d2 = new Date("2025-01-01");
if (d1 < d2) {
console.log("d1 is earlier");
}
📋 Summary Table
Method | Description |
---|---|
new Date() | Current date & time |
new Date(YYYY, MM, DD) | Specific date |
getFullYear() | Year |
getMonth() | Month (0–11) |
getDate() | Day of month (1–31) |
getDay() | Day of week (0–6) |
getTime() | Milliseconds since 1970 |
setFullYear(), setMonth() | Modify date |
toDateString() | Date in readable format |
toISOString() | Standard ISO format |
toLocaleDateString() | Localized format (e.g., DD/MM/YYYY) |
अभ्यास प्रश्न
- JavaScript में month का index कब से शुरू होता है और क्यों?
- नीचे दिए गए code का output बताइए:
const d = new Date(2025, 11, 25); console.log(d.toDateString());
- एक ऐसा function बनाइए जो आज की तारीख को “DD-MM-YYYY” format में return करे।
getDay()
method क्या return करता है?- क्या आप
Date
object को compare कर सकते हैं? एक उदाहरण दीजिए। getTime()
method किस काम आता है?