JavaScript Get Current Date yyyy-mm-dd & Time

JavaScript Get Current Date yyyy-mm-dd & Time

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

Get the current date and time in javascript. Here you will learn how to get the current date and time in JavaScript.

JavaScript Get Current Date yyyy-mm-dd & Time

Using JavaScript Date() method to get current date and time. It is an inbuilt method of javascript for getting date and time. 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:

Recommended JavaScript Tutorials

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

AuthorAdmin

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, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL 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 *