var date = new Date();

alert( date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2) + ("0" + date.getHours() ).slice(-2) + ("0" + date.getMinutes()).slice(-2) + ("0" + date.getSeconds()).slice(-2) );

edit

function pad2(n) { return n < 10 ? '0' + n : n }

var date = new Date();
    
alert( date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2( date.getDate()) + pad2( date.getHours() ) + pad2( date.getMinutes() ) + pad2( date.getSeconds() ) );
Answer from gurvinder372 on Stack Overflow
🌐
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
There are many ways to format a date as a string. The JavaScript specification only specifies one format to be universally supported: the date time string format, a simplification of the ISO 8601 calendar date extended format.
🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format:
🌐
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()); ... When formatting the time you are rounding every 5 minutes.
🌐
JSFiddle
jsfiddle.net › felipekm › MYpQ9
YYYYMMDDHHMMSS Date Format - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
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));

🌐
W3docs
w3docs.com › javascript
How to Format a JavaScript Date
You should use dateformat as a method and pass the Date object: 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'
Find elsewhere
🌐
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 - 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). The `moment()` function generates a moment object representing the current date ...
🌐
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(ti
🌐
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.
🌐
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.
🌐
Day.js
day.js.org › docs › en › display › format
Format · Day.js
dayjs().format() // current date in ISO8601, without fraction seconds e.g.
🌐
Zuga
zuga.net › articles › javascript-date-the-tostring-methods
Zuga.net | Javascript - Date() - the toString() methods
The Javascript Date type · doesn't support custom format strings like "yyyyMMddHHmmss". But it does provide a number of methods that return the date as a string in a specific format.
🌐
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) ...
🌐
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 - Use .slice() method to format the day, month to 2 digits. Example: This example implements the above approach. ... <body> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="gfg_Run()"> Click Here </button> <p ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Date and time
The method Date.parse(str) can read a date from a string. The string format should be: YYYY-MM-DDTHH:mm:ss.sssZ, where: