Actually, you don't need to perform any such conversion.

The string you have is already in very nearly an acceptable format for new Date(string) and in fact even as is will be accepted by most (if not all) modern browsers.

To make it fully compliant with the ISO8601 variant that ES5 mandates, just replace the space between the date and time with a literal T character, and add a time-zone specifier at the end (e.g. either Z for UTC, or +01:00, for example).

See http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 for more.

Answer from Alnitak on Stack Overflow
🌐
GitHub
gist.github.com › Ivlyth › c4921735812dd2c0217a
format javascript date to format "YYYY-mm-dd HH:MM:SS" · GitHub
function NOW() { var date = new Date(); var aaaa = date.getUTCFullYear(); var gg = date.getUTCDate(); var mm = (date.getUTCMonth() + 1); if (gg < 10) gg = "0" + gg; if (mm < 10) mm = "0" + mm; var cur_day = aaaa + "-" + mm + "-" + gg; var hours = date.getUTCHours() var minutes = date.getUTCMinutes() var seconds = date.getUTCSeconds(); if (hours < 10) hours = "0" + hours; if (minutes < 10) minutes = "0" + minutes; if (seconds < 10) seconds = "0" + seconds; return cur_day + " " + hours + ":" + minutes + ":" + seconds; } console.log(NOW());
🌐
Medium
trymysolution.medium.com › javascript-date-as-in-yyyy-mm-dd-hh-mm-ss-format-or-mm-dd-yyyy-hh-mm-ss-a0c96e8fa888
JavaScript Date as in YYYY-MM-DD hh:mm:ss Format or MM/DD/YYYY hh:mm:ss | by Yogesh D V | Medium
April 11, 2023 - function padTwoDigits(num: number) { return num.toString().padStart(2, "0"); } function dateInYyyyMmDdHhMmSs(date: Date, dateDiveder: string = "-") { // :::: Exmple Usage :::: // The function takes a Date object as a parameter and formats the ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
DD is the day of the month, with two digits (01 to 31). Defaults to 01. T is a literal character, which indicates the beginning of the time part of the string. The T is required when specifying the time part. HH is the hour, with two digits (00 to 23). As a special case, 24:00:00 is allowed, and is interpreted as midnight at the beginning of the next day. Defaults to 00. mm is the minute, with two digits (00 to 59). Defaults to 00. ss is the second, with two digits (00 to 59).
Top answer
1 of 4
181

[Addendum 12/2022]: Here's a library to format dates using Intl.DateTimeFormat.

[Addendum 01/2024]: And here is a (ES-)Date manipulation library

Try something like this

var d = new Date,
    dformat = [d.getMonth()+1,
               d.getDate(),
               d.getFullYear()].join('/')+' '+
              [d.getHours(),
               d.getMinutes(),
               d.getSeconds()].join(':');

If you want leading zero's for values < 10, use this number extension

Number.prototype.padLeft = function(base,chr){
    var  len = (String(base || 10).length - String(this).length)+1;
    return len > 0? new Array(len).join(chr || '0')+this : this;
}
// usage
//=> 3..padLeft() => '03'
//=> 3..padLeft(100,'-') => '--3' 

Applied to the previous code:

var d = new Date,
    dformat = [(d.getMonth()+1).padLeft(),
               d.getDate().padLeft(),
               d.getFullYear()].join('/') +' ' +
              [d.getHours().padLeft(),
               d.getMinutes().padLeft(),
               d.getSeconds().padLeft()].join(':');
//=> dformat => '05/17/2012 10:52:21'

See this code in [jsfiddle][1]

[edit 2019] Using ES20xx, you can use a template literal and the new padStart string extension.

const dt = new Date();
const padL = (nr, len = 2, chr = `0`) => `${nr}`.padStart(2, chr);

console.log(`${
    padL(dt.getMonth()+1)}/${
    padL(dt.getDate())}/${
    dt.getFullYear()} ${
    padL(dt.getHours())}:${
    padL(dt.getMinutes())}:${
    padL(dt.getSeconds())}`
);

2 of 4
73

You can always format a date by extracting the parts and combine them using string functions in desired order:

function formatDate(date) {
  let datePart = [
    date.getMonth() + 1,
    date.getDate(),
    date.getFullYear()
  ].map((n, i) => n.toString().padStart(i === 2 ? 4 : 2, "0")).join("/");
  let timePart = [
    date.getHours(),
    date.getMinutes(),
    date.getSeconds()
  ].map((n, i) => n.toString().padStart(2, "0")).join(":");
  return datePart + " " + timePart;
}

let date = new Date();
console.log("%o => %s", date, formatDate(date));

🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-format-date-yyyy-mm-dd-hh-mm-ss
Format a Date as YYYY-MM-DD hh:mm:ss in JavaScript | bobbyhadz
... Copied!console.log(['05', '24', ... // 👉️ '08:13:56' We used the addition (+) operator to add a space in the middle of the strings to get the date and time formatted as YYYY-MM-DD hh:mm:ss....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-format-datetime-to-yyyy-mm-dd-hhmmss-in-momentjs
How to Format Datetime to YYYY-MM-DD HH:MM:SS in Moment.js? - GeeksforGeeks
July 23, 2025 - In this approach, we are using the format method directly to convert the input date string from 'MM/DD/YYYY HH:mm:ss' format to 'YYYY-MM-DD HH:mm:ss'. By parsing the input date with the specified format and then formatting it using format(), we ...
🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
ISO dates can be written with added hours, minutes, and seconds (YYYY-MM-DDTHH:MM:SSZ): const d = new Date("2015-03-25T12:00:00Z"); Try it Yourself » · Date and time is separated with a capital T. UTC time is defined with a capital letter Z. If you want to modify the time relative to UTC, remove the Z and add +HH:MM or -HH:MM instead:
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-a-date-object-to-string-with-format-hh-mm-ss-in-javascript
How to convert a date object to string with format hh:mm:ss in JavaScript?
The Moment.JS library for the date ... requirements. Users can follow the syntax below to use the moment().format() method. let date = moment(); let dateStr = date.format("YY-MM-DD HH:mm:ss");...
🌐
Byby
byby.dev › js-format-date
How to parse and format a date in JavaScript
- ISO 8601: YYYY-MM-DDTHH:mm:ss.sssZ (e.g. 2022-05-30T00:00:00.000Z) - Short date: mm/dd/yyyy or dd/mm/yyyy (e.g. 04/24/2023 or 24/04/2023) - Long date: MMMM dd, yyyy (e.g. April 24, 2023) - RFC 2822: EEE, dd MMM yyyy HH:mm:ss GMT (e.g. Mon, 24 Apr 2023 00:00:00 GMT) - Unix timestamp: the number ...
🌐
MSR
rajamsr.com › home › javascript date format yyyy-mm-dd: easy way to format
Javascript Date Format YYYY-MM-DD: Easy Way to Format | MSR - Web Dev Simplified
March 3, 2024 - To format a JavaScript date using toLocaleDateString() with the format yyyy-mm-dd hh:mm:ss, you can use the following code. Add the options parameter values for the time component and replace the comma from the result string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-format-current-date-in-mm-dd-yyyy-hhmmss-format-using-javascript
How to format current date in MM/DD/YYYY HH:MM:SS format using ...
July 12, 2025 - <body> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="gfg_Run()"> Click Here </button> <p id="GFG_DOWN"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var d = new Date(); el_up.innerHTML = "Click on the button to format" + " the date accordingly.<br>Date = " + d; Number.prototype.padding = function(base, chr) { var len = (String(base || 10).length - String(this).length) + 1; return len > 0 ?
🌐
Node-RED
discourse.nodered.org › general
Format date at 'YYYY:MM:DD HH:MM;SS' - General - Node-RED Forum
November 23, 2023 - Hello. How can you format the date in the logs? now I have it '23 Nov 2023 hh:mm:ss' it must have been so 'YYYY:MM:DD HH:MM;SS' Writing a function just adds the date again(( const currentDate = new Date(); const formattedDateTime = currentDate.toISOString().replace(/T/, ' ').replace(/\..+/, ''); const { loggingLevel } = msg; delete msg.loggingLevel; const messageToLog = `${formattedDateTime} - [info] [function:Log message to system console] ${JSON.stringify(msg)}`; if (loggingLevel === '...
🌐
Sling Academy
slingacademy.com › article › javascript-get-and-format-current-date-time
JavaScript: Get current date time in yyyy/MM/dd HH:mm:ss format - Sling Academy
yyyy/MM/dd HH:mm:ss format. We’ll write code from scratch without using any third-party libraries. ... getSeconds() methods to get the current year, month, date, hour, minute, and second, respectively. To make sure the month, date, hour, minute, and second always are in the two-character format, we will use the string method
Top answer
1 of 2
37

You can fully format the string as mentioned in other posts. But I think your better off using the locale functions in the date object?

var d = new Date("2017-03-16T17:46:53.677"); 
console.log( d.toLocaleString() ); 

edit :

ISO 8601 ( the format you are constructing with ) states the time zone is appended at the end with a [{+|-}hh][:mm] at the end of the string.

so you could do this :

var tzOffset = "+07:00" 
var d = new Date("2017-03-16T17:46:53.677"+ tzOffset);
console.log(d.toLocaleString());
var d = new Date("2017-03-16T17:46:53.677"); //  assumes local time. 
console.log(d.toLocaleString());
var d = new Date("2017-03-16T17:46:53.677Z"); // UTC time
console.log(d.toLocaleString());

edit :

Just so you know the locale function displays the date and time in the manner of the users language and location. European date is dd/mm/yyyy and US is mm/dd/yyyy.

var d = new Date("2017-03-16T17:46:53.677");
console.log(d.toLocaleString("en-US"));
console.log(d.toLocaleString("en-GB"));

2 of 2
10

Here we go:

var today = new Date();
var day = today.getDate() + "";
var month = (today.getMonth() + 1) + "";
var year = today.getFullYear() + "";
var hour = today.getHours() + "";
var minutes = today.getMinutes() + "";
var seconds = today.getSeconds() + "";

day = checkZero(day);
month = checkZero(month);
year = checkZero(year);
hour = checkZero(hour);
minutes = checkZero(minutes);
seconds = checkZero(seconds);

console.log(day + "/" + month + "/" + year + " " + hour + ":" + minutes + ":" + seconds);

function checkZero(data){
  if(data.length == 1){
    data = "0" + data;
  }
  return data;
}

🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Date and time
The string format should be: YYYY-MM-DDTHH:mm:ss.sssZ, where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter. HH:mm:ss.sss – is the time: hours, minutes, seconds and milliseconds.