Today

Fri Jun 04 2021 09:02:39 GMT+0530 (India Standard Time)


JavaScript में date show करने Date() class का object बनाना पड़ता है जो date की full text string return करता है। By Default , date show कराने के लिए JavaScript browser का Time zone use करती है।

JS Date() Example

File : js_date.html

CopyFullscreenClose FullscreenRun
<!DOCTYPE html>
<html>
  <body>
    <script>
      let today = new Date();
      document.write(today);
    </script>
  </body>
</html>
Output
Fri Jun 04 2021 09:02:39 GMT+0530 (India Standard Time)

Date Class का जब Object बनाते हैं तो हमें current date & time as a string मिलता है जो कि new Date().toString() के जैसे ही होता है।

/*it will generate same output */
<script>
   let today = new Date();
   document.write(today.toString());
</script>

JS Cretae Date

हालाँकि Date Class का Object बनाते समय अगर आप constructor में date pass करके new date भी बना सकते हैं। JavaScript में हम 3 तरीके से Date बना सकते हैं।
For Example :

new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)

JS Create Date Example

File : js_create_date.html

CopyFullscreenClose FullscreenRun
<!DOCTYPE html>
<html>
  <body>
    <script>
      var d = new Date(2029, 11, 24, 10, 33, 30);
      document.write(d.getFullYear()+"<br>");
      document.write(d.getMonth()+"<br>");
      document.write(d.getDate()+"<br>");
      document.write(d.getHours() +"<br>");
      document.write(d.getMinutes() +"<br>");
      document.write(d.getSeonds() +"<br>");
    </script>
  </body>
</html>
Output
2029
11
24
10
33

JS Date Static Methods

Date Class के कुछ static methods हैं , जिन्हे है बिना Date Class का Object बनाये call करा सकते हैं।

Method Description
Date.now() now() method current time को as a numeric value return करता है।
parse() Date.parse() method , string date को parse करके , 1 जनवरी, 1970, 00:00:00 UTC . के बाद से milliseconds की संख्या return करता है।

JS Date Instance Methods

static methods के अलावा JavaScript में Date class के कुछ instance methods भी , जिन्हे access करने के लिए Date Class का Object बनाना पड़ता है।

Method Description
getFullYear() getFullYear() methods local time के according year (4 digit  में ) return करता है।
getMonth() getMonth() method local time के according month (0–11) return करता है।
getDate() getDate() method local time के according month का day number (1 - 31) return करता है।
getDay() getDay() method local time के according week का day number (0 - 6) return करता है।
getHours() getHours() method local time के according hour (0 - 23) return करता है।
getMinutes() getMinutes() method local time के according minutes (0 - 59) return करता है।
getSeconds() getSeconds() method local time के according seconds (0 - 59) return करता है।
setFullYear() setFullYear() methods local time के according दी गयी date के लिए year set करता है।
setMonth() setMonth() method local time के according दी गयी date के लिए month set करता है।
setDate() setDate() method local time के according दी गयी date के लिए month का day set करता है।
setHours() setHours() method local time के according दी गयी date के लिए hour set करता है।
setMinutes() setMinutes() method local time के according दी गयी date के लिए minutes set करता है।
setSeconds() setSeconds() method local time के according दी गयी date के लिए seconds set करता है।

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers