If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
JavaScript में हम Date format को easily modify कर सकते हैं , generally JavaScript में 3 तरह के Date formats use होते हैं।
Type | Example |
---|---|
ISO Date | 2020-05-25 (The International Standard) |
Short Date | 05/25/2020 |
Long Date | May 25 2015 or 25 May 2020 |
By Default JavaScript , Date को full text string format में return करती है।
File : js_date.html
<!DOCTYPE html>
<html>
<body>
<script>
let today = new Date();
document.write(today);
</script>
</body>
</html>
Fri Jun 04 2021 09:01:50 GMT+0530 (India Standard Time)
ISO 8601 ने Date represent करने के लिए standard mention किया हुआ है। JavaScript भी ISO 8601 syntax (YYYY-MM-DD) को prefer करती है।
हालाँकि date output आपके browser के timezone के according होगा , इसलिए किसी एक दिन के लिए ही time अलग अलग है।
File : js_iso_date.html
<!DOCTYPE html>
<html>
<body>
<script>
let d = new Date('2021-05-30');
document.write(d);
</script>
</body>
</html>
Sun May 30 2021 05:30:00 GMT+0530 (India Standard Time)
अब अगर आपको सिर्फ , Year - Month या सिर्फ Year show करानी होतो , आप need के according date pass कर सकते हैं।
For Example :
/*ISO Dates (Year and Month)*/ document.write(new Date("2021-05")); /*ISO Dates (only Year)*/ document.write(new Date("2021"));Output
Sat May 01 2021 05:30:00 GMT+0530 (India Standard Time) Fri Jan 01 2021 05:30:00 GMT+0530 (India Standard Time)
JavaScript में short date को MM/DD/YYYY के format में लिखा जाता है।
<script>
document.write(new Date("04/25/2021"));
</script>Output
Sun Apr 25 2021 00:00:00 GMT+0530 (India Standard Time)
? Date format DD-MM-YYYY && YYYY/MM/DD invalid formats हैं इसलिए इनके use से बचे।
Long date को ज्यादातर MMM DD YYYY के रूप में लिखा जाता है। हालाँकि इसमें Month & date का order आप change कर सकते हैं।
File : js_long_date.html
<!DOCTYPE html>
<html>
<body>
<script>
document.write(new Date("May 25 2021"));
/*change month-day order*/
document.write(new Date("25 May 2021"));
/*we can write like this*/
document.write(new Date("January 25 2021"));
/*we can also write month like this*/
document.write(new Date("Jan 25 2021"));;
</script>
</body>
</html>
Tue May 25 2021 00:00:00 GMT+0530 (India Standard Time) Tue May 25 2021 00:00:00 GMT+0530 (India Standard Time) Mon Jan 25 2021 00:00:00 GMT+0530 (India Standard Time) Mon Jan 25 2021 00:00:00 GMT+0530 (India Standard Time)
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook