[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())}`
);

Answer from KooiInc on Stack Overflow
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));

🌐
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):
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
Date-time form: one of the above date-only forms, followed by T, followed by HH:mm, HH:mm:ss, or HH:mm:ss.sss.
🌐
Day.js
day.js.org › docs › en › display › format
Format · Day.js
dayjs().format() // current date in ISO8601, without fraction seconds e.g. '2020-04-02T08:02:17-05:00' dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]') // 'YYYYescape 2019-01-25T00:00:00-02:00Z' dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
🌐
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());
🌐
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....
🌐
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 - // 👇️ 2023-04-11 16:21:23 (yyyy-mm-dd hh:mm:ss) //console.log(dateInYyyyMmDdHhMmSs(new Date())); // 👇️️ 2025-05-04 05:24:07 (yyyy-mm-dd hh:mm:ss) // console.log(dateInYyyyMmDdHhMmSs(new Date('May 04, 2025 05:24:07'))); // Date divider // 👇️ 01/04/2023 10:20:07 (MM/DD/YYYY hh:mm:ss...
🌐
Moment.js
momentjs.com › docs
Moment.js | Docs
moment('24/12/2019 09:15:00', "DD MM YYYY hh:mm:ss");
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › how-to-format-the-current-date-in-mm-dd-yyyy-hhmmss-format-using-node-js
How to format the current date in MM/DD/YYYY HH:MM:SS format using Node? - GeeksforGeeks
July 23, 2025 - const moment = require("moment"); // 24 Hour format console.log( moment() .format("MM/DD/YYYY HH:mm:ss") ); // 12 Hour format console.log( moment() .format("MM/DD/YYYY hh:mm:ss") );
Find elsewhere
🌐
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 ...
🌐
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?
</h2> <h4> Using the <i> moment().format() </i> method to convert the time in the hh : mm : ss format </h4> <p id = "output1"> </p> <script> let output1 = document.getElementById("output1"); let date = moment(); let dateStr = date.format("YY-MM-DD HH:mm:ss"); output1.innerHTML += "Current date is : " + dateStr + " <br/> "; </script> </body> </html>
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;
}

🌐
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
For example, 1 => 01, 2 => 02 let date = dateObj.getDate(); date = ('0' + date).slice(-2); // To make sure the date always has 2-character-format let hour = dateObj.getHours(); hour = ('0' + hour).slice(-2); // To make sure the hour always has 2-character-format let minute = dateObj.getMinutes(); minute = ('0' + minute).slice(-2); // To make sure the minute always has 2-character-format let second = dateObj.getSeconds(); second = ('0' + second).slice(-2); // To make sure the second always has 2-character-format const time = `${year}/${month}/${date} ${hour}:${minute}:${second}`; console.log(time);
🌐
LogRocket
blog.logrocket.com › home › how to format dates in javascript: methods, libraries, and best practices
How to format dates in JavaScript: Methods, libraries, and best practices - LogRocket Blog
May 8, 2025 - function formatDate(date, format) { const day = String(date.getDate()).padStart(2, '0'); const month = String(date.getMonth() + 1).padStart(2, '0'); const year = date.getFullYear(); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0'); // Replace tokens with actual values return format .replace('YYYY', year) .replace('MM', month) .replace('DD', day) .replace('HH', hours) .replace('mm', minutes) .replace('ss', seconds); } const date = new Date('2025-02-18T14:30:45Z'); console.log(formatDate(date, 'YYYY-MM-DD')); // "2025-02-18" console.log(formatDate(date, 'DD/MM/YYYY HH:mm:ss')); // "18/02/2025 14:30:45"
🌐
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 - Given a date and the task is to format the current date in MM/DD/YYYY HH:MM:SS format. Here are a few of the most techniques discussed with the help of JavaScript.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-3.php
JavaScript: Display the current date in various format - w3resource
// Get the current date var today ... (mm < 10) { mm = '0' + mm; } // Format the date as mm-dd-yyyy and log it today = mm + '-' + dd + '-' + yyyy; console.log(today); // Format the date as mm/dd/yyyy and log it today = mm + '/' ...
🌐
GitHub
gist.github.com › mohokh67 › e0c5035816f5a88d6133b085361ad15b
Get YYYY-MM-DD HH-MM-SS in JavaScript · GitHub
Get YYYY-MM-DD HH-MM-SS in JavaScript. GitHub Gist: instantly share code, notes, and snippets.