[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));

🌐
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) ... string = "-") { // :::: Exmple Usage :::: // The function takes a Date object as a parameter and formats the date as YYYY-MM-DD hh:mm:ss....
🌐
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).
🌐
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:
🌐
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
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 - We first convert the input date string into a Unix timestamp, then use moment.unix() to create a new Moment.js object from this timestamp. Finally, we format this object to 'YYYY-MM-DD HH:mm:ss', resulting in the desired date format.
🌐
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 · Raw · timestamp-formatter.md · const formatedTimestamp = ()=> { const d = new Date() const date = d.toISOString().split('T')[0]; const time = d.toTimeString().split(' ')[0].replace(/:/g, '-'); return `${date} ${time}` } Sign up for free to join this conversation on GitHub.
Find elsewhere
🌐
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 === '...
🌐
W3docs
w3docs.com › javascript
How to Format a JavaScript Date
const dateformat = require('dateformat'); let now = new Date(); dateformat(now, 'dddd, mmmm dS, yyyy, h:MM:ss TT'); 'Tuesday, Feb 2nd, 2020, 4:30:20 PM'
🌐
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");...
🌐
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 - This code utilizes the `moment` library to format the current date and time. The first `console.log` prints the date and time in the MM/DD/YYYY HH:mm:ss format (24-hour clock), while the second one uses the hh:mm:ss format (12-hour clock).
🌐
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
This concise article shows you how to get the current date and time in Javascript in yyyy/MM/dd HH:mm:ss format. We’ll write code from scratch without using any third-party libraries. In the example below, we will create a new...
🌐
ServiceNow Community
servicenow.com › community › developer-forum › show-current-date-time-in-yyyy-mm-dd-hh-mm-ss-format-for-a-date › td-p › 2754288
Show current date/time in YYYY-MM-DD HH:MM:SS format for a date/time field using Client Script
December 6, 2023 - var d = new Date(); // Check your condition if (newValue == 'true') { // Format the date into YYYY-MM-DD HH:MM:SS var formattedDate = d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + // Months are zero-based ('0' + d.getDate()).slice(-2) + ' ' + ('0' + d.getHours()).slice(-2) ...
🌐
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 - The slice() method extracts the date and time portion of the string yyyy-mm-dd hh:mm:ss and replaces the ‘T’ separator with an empty space.
🌐
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 ...
🌐
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 JavaScript ? - GeeksforGeeks
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.