JavaScript | Date.prototype property

The Date.prototype property represents the prototype for the Date constructor.
It has the following methods-
1) getDate(): This method will return the day of the month for the specified date according to the local time.
2) getDay(): This method will return the day of the week (0 for Sunday and 6 for Saturday) for the specified date according to the local time.
3) getFullYear(): It returns the year for the specified date according to local time.
4) getHours(): It returns the hour(0-23) for the specified date according to local time.
5) getMilliseconds: It returns the milliseconds (0-999) for the specified date according to local time.
6) getMinutes(): It returns the minutes (0-59) for the specified date according to local time.
7) getMonth(): It returns the month (0-11) for the specified date according to local time.
8) getSeconds(): It returns the seconds(0-59) for the specified date according to local time.
9) getTime(): It returns the number of milliseconds ellapsed since January 01, 1970 00:00:00 UTC. It is negative for time before the given time.
10) getTimeozneOffset(): It returns the timezone offset in minutes for current location.
11) getUTCDate(): It returns the date of the month (1-31) for the specified date according to universal time.
12) getUTCDay(): It returns the day of the week (0-6) for the specified date according to universal time.
13) getUTCFullYear(): It returns the year for the specified date according to universal time.
14) getUTCHours(): It returns the hours (0-23) for the specified date according to universal time.
15) getUTCMilliseconds(): It returns the milliseconds(0-999) for the specified date according to universal time.
16) getUTCMinutes(): It returns the minutes (0-59) for the specified date according to universal time.
17) getUTCMonth(): It returns the month (0-11) for the specified date according to universal time.
18) getUTCSeconds(): It returns the seconds (0-59) for the specified date according to universal time.

JavaScript Code to show the working of above methods:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    
  var birthday = new Date('June 21, 2018 16:44:23');
  var date1 = birthday.getDate(); 
  var day1 = birthday.getDay(); 
  var year1 = birthday.getFullYear(); 
  var hour1 = birthday.getHours(); 
  var ms1 = birthday.getMilliseconds(); 
  var m1 = birthday.getMinutes(); 
  var mth1 = birthday.getMonth(); 
  var time1 = birthday.getTime(); 
  var s1 = birthday.getSeconds(); 
  var offset = birthday.getTimezoneOffset(); 
  var date2 = birthday.getUTCDate(); 
  var day2 = birthday.getUTCDay(); 
  var year2 = birthday.getUTCFullYear(); 
  var hour2 = birthday.getUTCHours(); 
  var ms2 = birthday.getUTCMilliseconds();
  var um1 = birthday.getUTCMinutes();
  var umth = birthday.getUTCMonth();
  var us = birthday.getUTCSeconds();
  
  document.write(date1 +"<br>");
  
  document.write(day1 +"<br>");
  
  document.write(year1 +"<br>");
  
  document.write(hour1 +"<br>");
  
  document.write(ms1 +"<br>");
  
  document.write(m1 +"<br>");
  
  document.write(mth1 +"<br>");
  
  document.write(time1 +"<br>");
  
  document.write(s1 +"<br>");
  
  document.write(offset +"<br>");
  
  document.write(date2 +"<br>");
  
  document.write(day2 +"<br>");
  
  document.write(year2 +"<br>");
  
  document.write(hour2 +"<br>");
  
  document.write(ms2 +"<br>");
  
  document.write(um1 +"<br>");
  
  document.write(umth +"<br>");
  
  document.write(us);        
</script>

chevron_right


Output:

21
4
2018
16
0
44
5
1529579663000
23
-330
21
4
2018
11
0
14
5
23

It also has some other methods which can be used to convert the date to different formats:
1) toDateString(): Returns the “date” portion of the Date as a human-readable string.
2) toGMTString(): Returns a string representing the Date based on the GMT (UT) time zone.
3) toLocaleFormat(): Converts a date to a string, using a format string.
4) toLocalestring(): Returns a string with a locality sensitive representation of this date.
5) toString(): Returns a string representing the specified Date object.
6) toTimeString(): Returns the “time” portion of the Date as a human-readable string.
7) valueOf(): Returns the primitive value of a Date object.
For getting more details about all above individual functions click here.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.