Table of Contents
Introduction
हेलो दोस्तों आज हम इस post में javascript के Date() function के बारेमे जानगे!. Date() function का इस्तेमाल करके हम कैसे current date,time, current month ,current year को access करके और उसमे changes कर सकते है!.
Date() function in javascript
Javascript में Date() function developers को date और time के साथ सरलता पूर्वक काम करने में सक्षम बनता है, यह current time को get करने और time में specific time को create करने की अनुमति देता है!.
Javascript में Date() function time में specific moments का representing करने वाले Date object के creation की facility provide करता है, जिससे developer को dates manipulate करने, date components को retrieve करने और web applications में date related operation perform करने में सक्षम बनता है!.
Creating Date object
Current date और time को get करने के लिए Date object create करने के लिए निचे दिए गये syntax से कर सकते है!.
syntax:
<script> var currentDate = new Date(); var specificDate = new Date(2024, 1, 28, 12, 30, 0); // February 28, 2024, 12:30:00 PM </script>
ऊपर के syntax code में देख सकते है, यह Date object को initializes करता है और इसमें current date और time को holds करता है!.
Date object का इस्तेमाल करके हम current month, current month के day, current day, current hours or current minutes को get कैसे कर सकते है!.
- getDate() => यह month के day को return करता है!.
- getMonth() => यह month को return करता है, लेकिन इसमें Zero based month होते है, मतलब की january 0 or december 11 होते है!.
- getFullYear() => यह year return करता है!.
- getDay() => यह week का day return करता है जिसमे sunday 0 और saturday 6 होता है!.
- getHours() => यह hours(0-23) return करता है!.
- getMinutes() => यह minute(0-59) return करता है!.
Getting Day, Month, Year
Date() function का इस्तेमाल करके कैसे हम, current day, month और year को get कर सकते है, वह निचे example में देख सकते है।
Example:
<script> var currentDate = new Date(); console.log( currentDate.getDate() ); console.log( currentDate.getMonth() + 1); // Adding 1 to account for zero-based months console.log(currentDate.getFullYear()); </script>
Getting Day of the Week
Date() function का इस्तेमाल करके हम week के day को get करा सकते है, वह example में देख सकते है!.
Example
<script> var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var currentDate = new Date(); var dayOfWeek = days[currentDate.getDay()]; console.log(dayOfWeek); </script>
Getting Hours and Minutes
Date() function के इस्तेमाल करके हम current hours और minutes को get कर सकते है, वह example में देख सकते है!.
Example
<script> var currentDate = new Date(); console.log(currentDate.getHours()); console.log(currentDate.getMinutes()); </script>
Date() function का इस्तेमाल करके month का first day और last day कैसे get करा सकते है!.
- सबसे पहले हम Date() function का एक object create कर लेंगे!.
- उसके बाद में Date() function में year, month और “1” pass करेंगे month का first day get करने के लिए!
- उसके बाद month का last get करने के लिए Date() function में year, month और day के लिए “0” pass करेंगे और month का last day get करेंगे!.
Example
<script> function getFirstAndLastDateOfMonth(year, month) { // Create a new Date object with the given year and month (months are zero-based) var firstDate = new Date(year, month, 1); // Get the last day of the month by moving to the next month (month + 1) and going back one day (0) var lastDate = new Date(year, month + 1, 0); return { firstDate: firstDate, lastDate: lastDate }; } // Usage example: var year = 2024; var month = 1; // February (zero-based index) var dates = getFirstAndLastDateOfMonth(year, month); console.log("First Date of the Month:", dates.firstDate); console.log("Last Date of the Month:", dates.lastDate); </script>