JavaScript – Get Date Methods

JavaScript – Get Date Methods

Get-Date Functions/Methods in js. Here, we would like to share with you, javascript get date methods with their examples.

The Javascript get date methods are an inbuilt function in JavaScript which returns the years, months, days, hours, minutes, seconds, milliseconds from a Date Object.

You can use the javascript get methods for e.g. get current date and time in javascript, display current date and time in html using javascript get date methods.

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

When you run above script, the output will be:

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,

JavaScript Get Date Methods

The javascript get date methods are given below:

  • getFullYear()
  • getMonth()
  • getDate()
  • getHours()
  • getMinutes()
  • getSeconds()
  • getMilliseconds()
  • getDay()

1: The getFullYear() method

The method gives four digit number of the current year according to local time.

Example

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).

Example

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.

Example

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.

Example

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.

Example

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

You can create digital clock in javascript using the javascript get methods. Learn here, javaScript Digital Clock with date.

AuthorTuts Make

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *