We can use JavaScript to get the date in the format of yyyy-mm-dd hh mm ss by making use of the JavaScript toLocaleString() method. There are many configurations we can utilize with the toLocaleString() method to display the date how we want. One of these configurations will get the format we want.

Here is the code to get the date in the format of yyyy-mm-dd hh mm ss. We will explain how we got this below.

var currDate = new Date();
var localDate = currDate.toLocaleString('en-CA', {year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'});

console.log(localDate);

#Output
2022-09-01, 02:16:53 p.m.

The JavaScript toLocaleString method will take a date object and return the date as a string using the local settings from your computer.

var currDate = new Date();
var localDate = currDate.toLocaleString();

The date is returned in a much friendlier format for the user than what the Date() method returns. The outputs of currDate and localDate from above are displayed as follows:

currDate: Fri Jan 28 2022 21:35:02 GMT-0500 (Eastern Standard Time)
localDate: 1/28/2022, 9:35:02 PM

So if we wanted to display the date in the yyyy-mm-dd hh mm ss format, we can use different options for parameters in the toLocaleString() method. A list of these options can be found here.

So to get the date in the format of yyyy-mm-dd hh mm ss, we can simply use the Canadian English language format as the first parameter in the toLocaleString() method, and then how we want each date and time unit formatted as the second parameter. Let’s see this below.

var currDate = new Date();
var localDate1 = currDate.toLocaleString('en-CA', {year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'});

console.log(localDate1);

#Output
2022-09-01, 02:17:12 p.m.

If we want to display the date with slashes instead of hyphens, yyyy/mm/dd hh mm ss, we can use the following parameter:

var currDate = new Date();
var localDate3 = currDate.toLocaleString('en-ZA', {year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: 'true'});

console.log(localDate3);

#Output
2022/09/01, 02:17:54 pm

There are other languages we can put as the parameter that would also display the date in the yyyy-mm-dd hh mm ss format as well, we just chose the first one we saw down the list.

Displaying the Current Date and Time in YYYY-MM-DD HH MM SS using the JavaScript toLocaleString Method

Below we will provide code to get the current date in JavaScript, and let the user see it in a friendly format when they click a button.

Get Date

Today's Date is:

We will simply get the Date with the Date() method, and use the toLocaleString() method and some parameter options to display the date and time nicely. Here is the simple JavaScript code:

function genNewDate(){
  var currDate = new Date();
  var localDate = currDate.toLocaleString('en-CA', {year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit'});
  document.getElementById("theDate").innerHTML = localDate;
}

The final code and output for this example is below:

Code Output:

Get Date

Today’s Date is:

Full Code:

Get Date

Today's Date is:

Hopefully this article has been useful in helping you understand how to use JavaScript to get the date in the format of yyyy-mm-dd hh mm ss.

Categorized in:

JavaScript,

Last Update: March 11, 2024