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
🌐
GitHub
gist.github.com › Ivlyth › c4921735812dd2c0217a
format javascript date to format "YYYY-mm-dd HH:MM:SS" · GitHub
const formatedTimestamp = ()=> { const d = new Date() const date = d.toISOString().split('T')[0]; const time = d.toTimeString().split(' ')[0]; return `${date} ${time}` }
🌐
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.
🌐
JSFiddle
jsfiddle.net › felipekm › MYpQ9
YYYYMMDDHHMMSS Date Format - JSFiddle - Code Playground
For now it's a BYOK implmentation which means you need to provide your own API Key − you can get it for free.
🌐
W3docs
w3docs.com › javascript
How to Format a JavaScript Date
It operates with both Node.js and ...y').fromNow(); // 11 hours ago moment().endOf('day').fromNow(); // in 13 hours · The .format() method constructs a string of tokens that refer to a particular component of date (like day, month, ...
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › date-tostring-format-yyyy-mm-dd
Format a JavaScript Date to YYYY MM DD - Mastering JS
const date = new Date(); const year = date.getFullYear() * 1e4; // 1e4 gives us the the other digits to be filled later, so 20210000. const month = (date.getMonth() + 1) * 100; // months are numbered 0-11 in JavaScript, * 100 to move two digits ...
🌐
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' More available formats Q Do k kk X x ... in plugin AdvancedFormat · Because preferred formatting differs based on locale, there are a few localized format tokens that can be used based on its locale.
Find elsewhere
🌐
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 = "-") { return ( [ date.getFullYear(), padTwoDigits(date.getMonth() + 1), padTwoDigits(date.getDate()), ...
🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
Independent of input format, JavaScript will (by default) output dates in full text string format: ISO 8601 is the international standard for the representation of dates and times. The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format: const d = new Date("2015-03-25"); Try it Yourself » · The computed date will be relative to your time zone.
🌐
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 want to make sure that the result is always consistent and has 2 digits for the months, days, hours, minutes and seconds, so we used the String.padStart() method. We passed the following 2 arguments to the padStart() method: ... The padStart method will never pad the values to more than 2 characters because we set the target length to 2. We then created a function that takes a date and formats it to YYYY-MM-DD hh:mm:ss.
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));

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › toDateString
Date.prototype.toDateString() - JavaScript | MDN
const event = new Date(1993, 6, 28, 14, 39, 7); console.log(event.toString()); // Expected output: "Wed Jul 28 1993 14:39:07 GMT+0200 (CEST)" // Note: your timezone may vary console.log(event.toDateString()); // Expected output: "Wed Jul 28 1993" ... A string representing the date portion of the given date (see description for the format).
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-format-date-yyyymmdd
Format a Date as YYYY-MM-DD using JavaScript | bobbyhadz
The first parameter we passed to the padTo2Digits function is the total length of the string, so it will never pad the day or month if they already have 2 digits. Next, we created a function that takes a date and formats it to YYYY-MM-DD.
🌐
W3Schools
w3schools.com › jsref › jsref_toisostring.asp
JavaScript Date toISOString() Method
The toISOString() method returns a date object as a string, using the ISO standard. The standard is called ISO-8601 and the format is: YYYY-MM-DDTHH:mm:ss.sssZ · toISOString() is an ECMAScript5 (ES5 2009) feature.
🌐
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 then extract the relevant portion of the ISO string (up to the first 19 characters) and replace the 'T' separator with a space to format it as 'YYYY-MM-DD HH:mm:ss'. Example: The below example uses the format Method Directly to Format datetime ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › parse
Date.parse() - JavaScript | MDN
// Standard date-time string format const unixTimeZero = Date.parse("1970-01-01T00:00:00Z"); // Non-standard format resembling toUTCString() const javaScriptRelease = Date.parse("04 Dec 1995 00:12:00 GMT"); console.log(unixTimeZero); // Expected output: 0 console.log(javaScriptRelease); // Expected output: 818035920000
🌐
GitHub
gist.github.com › mohokh67 › e0c5035816f5a88d6133b085361ad15b
Get YYYY-MM-DD HH-MM-SS in JavaScript · GitHub
Save mohokh67/e0c5035816f5a88d6133b085361ad15b to your computer and use it in GitHub Desktop. ... const formatedTimestamp = ()=> { const d = new Date() const date = d.toISOString().split('T')[0]; const time = d.toTimeString().split(' ')[0].replace(/:/g, '-'); return `${date} ${time}` }
🌐
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.