javaScript Get Current Date Time

javaScript Get Current Date Time

To get the current date and time in javascript. This tutorial will explain to you, how to get the current date and time in JavaScript.

Sometimes, we work with javascript. We need to get the current date, time, month, year, etc. This tutorial explains you how to get date and time different formats like yyyy-mm-dd HH:ii:ss and y/m/d h:i:s.

JavaScript Get Current Date & Time

Using JavaScript Date() method to get the current date and time. It is inbuilt method of javascript for getting a date. By default, JavaScript will use the browser’s time zone and display a date as a full text string.

  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 2019 19: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

If you want to know more about javascript date and time methods, you may like following date and time methods:

You may like

  1. JavaScript: Date setUTCDate() Method
  2. Set UTC Month In javaScript
  3. setUTCFullYear() Method JavaScript With Examples
  4. javascript date setUTCHours() Method With Examples
  5. JavaScript: d.setUTCMinutes() Method e.g.
  6. setUTCSecond() Method By JavaScript
  7. javaScript Date set UTC milliseconds
  8. setUTCSecond() Method By JavaScript
  9. JavaScript: Date.getUTCDate() Method
  10. getUTCmonth Method javaScript With Example
  11. Digital Clock with date in javaScript
  12. JavaScript: Set Date Methods
  13. JavaScript Get Date Methods

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

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