Get-date methods in JavaScript; Through this tutorial, i am going to show you JavaScript get date methods with the help of examples.
JavaScript Get Date Methods
There are javascript get date methods; as shown below:
- getFullYear()
- getMonth()
- getDate()
- getHours()
- getMinutes()
- getSeconds()
- getMilliseconds()
- getDay()
- Example :- Get & Display Current Date and Time in Html using JavaScript
1 – The getFullYear() method
The method gives four digit number of the current year according to local time.
var date = new Date(); document.write('date = ' + date.getFullYear() +"<br>");
Output
// return current year
2019
2 – The getMonth() Method
The date.getMonth() is an inbuilt function of javascript. The getMonth() method returns the month of a date as a number (0-11).
var date = new Date(); document.write('month = ' + date.getMonth() +"<br>");
it is return current month
return //
2
3 – The getDate() Method
The JavaScript getDate() method returns the day of the month for the specified date according to local time. The getDate() method is return no an integer between 1 and 31.
var date = new Date(); document.write('date = ' + date.getDate() +"<br>");
Output
//Return current date
18
4 – The getHours() Method
The JavaScript getHours() method returns the hours of the day for the specified date according to local time. The getDate() method is return no an integer between 0 and 23.
var date = new Date(); document.write('Hours = ' + date.getHours() +"<br>");
Output
return hours of day
22
5 – The getMinutes() Method
The JavaScript getMinutes() method returns the minutes of the hour for the specified date using local time. The getDate() method is return no an integer between 0 and 59.
var date = new Date(); document.write('Minutes = ' + date.getMinutes() +"<br>");
output
return minutes of the hour
25
6 – The getSeconds() Method
The JavaScript getSeconds() method returns the seconds of a date using a local time and return num 0 to 59 in seconds.
var date = new Date(); document.write('Seconds = ' + date.getSeconds() +"<br>");
output
return second of the minute
35
7 – The getMilliseconds() Method
The JavaScript getMilliseconds() method returns the milliseconds of a date using a local time and return num 0 to 59 in milliseconds.
var date = new Date(); document.write('MilliSeconds = ' + date.getMilliseconds() +"<br>");
Output
return millisecond
55
8: The getDay() Method
The JavaScript getDay() method returns the weekday of a date using a local time and return num 0 to 6.
var date = new Date(); document.write('Day = ' + date.getDay() +"<br>");
output
return
1
Example :- Get & Display Current Date and Time in Html using JavaScript
You can see the below example to get and display current date and time in html using javascript get date methods:
<html> <head> <title>Display Current Date and Time in Html using Javascript</title> <script type="text/javascript"> var today = new Date(); var day = today.getDay(); var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"]; var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate(); var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); var dateTime = date+' '+time; document.write('current day = ' + daylist[day] +"<br>"); document.write('current date and time (YYYY-MM-DD HH:MM:SS) = ' + dateTime +"<br>"); </script> </head> <body> </body> </html>
Output of the above program:
current day = Saturday
current date and time (YYYY-MM-DD HH:MM:SS) = 2020-5-9 8:0:6
Many things may seem confusing to you in the above get and display current date and time in html using javascript at this point of time but do not worry you will be able to understand everything about get date method in JavaScript by the end of this tutorial.
You can observe that in the above display current date and time in html using javascript using get date methods, we have used the document.write statement.