How to Get Current Date and Time in JavaScript

To get the current date and time in javaScript. Through this tutorial, i am going to show you how to get the current date and time in JavaScript using built-in methods.

How to Get Current Date & Time in JavaScript

Using JavaScript Date() method to get current date and time. Let’s see the following example; as follows:

  var date = new Date();
  document.write( "JavaScript Get Current Date & Time : " + date );

If you can use the above javascript code, it gives output like this below:

JavaScript Get Current Date & Time : Mon Nov 25 2021:33:33 WITH Standard Format

1 – javascript Get a Current Date without Time

To get the current date in format yyyy-mm-dd . You can use the below-given javascript code for getting current date in yyyy-mm-dd format without time.

var cDate = new Date();
var date = cDate.getFullYear()+'-'+(cDate.getMonth()+1)+'-'+cDate.getDate();

Here,

  • getFullYear() Method – This method will provide current year,.
  • getMonth() Method – This method provide current month values between 0-11. Where 0 for Jan, 1 for Feb, and so on. That’s why we have added +1 to result.
  • getDate() Method – This method will provide day of the month values between 1-31.

2 – JavaScript Get Current Time

To get the current time in format H:i:s . You can use the below given javascript code for getting the current time in H:i:s format.

var cDate = new Date();
var time = cDate.getHours() + ":" + cDate.getMinutes() + ":" + cDate.getSeconds();

Here,

  • getHours() Method – This method provide current hour value between 0-23.
  • getMinutes() Method – This method provide current minutes value between 0-59.
  • getSeconds() Method – This method provide current seconds value between 0-59.

3 – JavaScript Get The Current Date and Time

If you want to get current date and time format yyyy-mm-dd H:i:s format in javascript. So you can use the below javascript code for getting the current date and time format yyyy-mm-dd H:i:s.

In this example we have got current date and time separated after that it has merge and display.

var cDate = new Date();
var date = cDate.getFullYear()+'-'+(cDate.getMonth()+1)+'-'+cDate.getDate();
var time = cDate.getHours() + ":" + cDate.getMinutes() + ":" + cDate.getSeconds();
var dateTime = date+' '+time;

Result of above javascript coode is:

 JavaScript Get Current Date & Time : 2019-11-25 19:28:24

More JavaScript Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.