const format1 = "YYYY-MM-DD HH:mm:ss"
const format2 = "YYYY-MM-DD"
var date1 = new Date("2020-06-24 22:57:36");
var date2 = new Date();

dateTime1 = moment(date1).format(format1);
dateTime2 = moment(date2).format(format2);

document.getElementById("demo1").innerHTML = dateTime1;
document.getElementById("demo2").innerHTML = dateTime2;
<!DOCTYPE html>
<html>
<body>

<p id="demo1"></p>
<p id="demo2"></p>

<script src="https://momentjs.com/downloads/moment.js"></script>

</body>
</html>

Answer from Jayram on Stack Overflow
🌐
Moment.js
momentjs.com
Moment.js | Home
moment().format('MMMM Do YYYY, h:mm:ss a'); moment().format('dddd'); moment().format("MMM Do YY"); moment().format('YYYY [escaped] YYYY'); moment().format(); moment("20111031", "YYYYMMDD").fromNow(); moment("20120620", "YYYYMMDD").fromNow(); moment().startOf('day').fromNow(); moment().endO...
🌐
Moment.js
momentjs.com › docs
Moment.js | Docs
They work the same as the single format case. moment("29-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], 'fr'); // uses 'fr' locale moment("29-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], true); // uses strict parsing moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"], 'fr', true); // uses 'fr' locale and strict parsing
🌐
Obsidian
forum.obsidian.md › help
Moment.js date issue for specific formats (/w dashes) - Help - Obsidian Forum
November 4, 2023 - What I’m trying to do I’m attempting to use the following dataviewjs inline query. That takes the files created today using a YAML header that is generated using templater in the format shown below and counts them. $=dv.pages().where(f=>f.date == moment().format('YYYY-MM-DD')).length All my “date” headers are in the YYYY-MM-DD format which for today would be “2023-11-04” Things I have tried I know that the right output is given when using the moment().format(‘YYYY-MM-DD’) code in the terminal...
🌐
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.
🌐
Readthedocs
momentjscom.readthedocs.io › en › latest › moment › 04-displaying › 01-format
Format - momentjs.com
moment().format(); // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds) moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm" moment().format("ddd, hA"); // "Sun, 3PM" moment().format("[Today is] dddd"); // "Today is Sunday" moment('gibberish').format('YYYY MM DD'); // "Invalid date" X was added in 2.0.0.
🌐
Devhints
devhints.io › javascript libraries › moment.js cheatsheet
Moment.js cheatsheet
m = moment('2013-03-01', 'YYYY-MM-DD') This parses the given date using the given format. Returns a moment object.
🌐
GitHub
github.com › moment › moment › issues › 4300
moment().format("YYYY-MM-DDTHH:mm:SS"); returns "" · Issue #4300 · moment/moment
November 11, 2017 - Description of the Issue and Steps to Reproduce: just run execute this line moment().format("YYYY-MM-DDTHH:mm:SS"); and I sometimes get empty string as result. Please include the values of all variables used. Environment: google chrome 6...
Published   Nov 11, 2017
🌐
GitHub
gist.github.com › brandonjp › ac259099ba95868c4826fc0f58f9e7b4
List of common date time formats for Moment.js · GitHub
moment-formats.md · YYYY YYYYMM YYYY-MM M/D MM/DD YYYYMMDD YYYY-MM-DD YYYY_MM_DD YYYY.MM.DD M/D/YY MM/DD/YY MM/DD/YYYY DD MMM YY DD MMM YYYY DD MMMM YYYY MMM D, YY MMM D, YYYY MMM DD, YYYY MMMM D, YYYY MMMM DD, YYYY YYYYMMDDHHmm YYYYMMDD_HHmm YYYY.MM.DD.HHmm YYYY-MM-DD-HHmm YYYY-MM-DD_HHmm YYYY.MM.DD.HH.mm YYYY-MM-DD-HH-mm YYYY-MM-DD HH:mm YYYY-MM-DD h:mm A YYYY-MM-DD hh:mm A YYYY-MM-DD @ h:mm A YYYYMMDDHHmmss YYYY.MM.DD.HHmmss YYYY-MM-DD-HHmmss YYYY-MM-DD_HHmmss YYYY-MM-DD_HHmm.ss YYYY.MM.DD.HH.mm.ss YYYY-MM-DD-HH-mm-ss YYYY-MM-DD HH:mm:ss YYYY-MM-DD HH:mm.ss YYYY-MM-DD h:mm:ss A YYYY-MM-DD
Find elsewhere
🌐
SitePoint
sitepoint.com › blog › javascript › managing dates and times using moment.js
Managing Dates and Times Using Moment.js — SitePoint
November 7, 2024 - const moment = require('moment'); ... '%d minutes', h : 'une heure', hh : '%d heures', d : 'un jour', dd : '%d jours', M : 'un mois', MM : '%d mois', y : 'un an', yy : '%d ans' } }); moment.locale('fr'); console.log(moment(131...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-format-date-with-momentjs
How to Format Date with Moment.js? - GeeksforGeeks
July 29, 2024 - const moment = require('moment'); console.log(moment().format('YYYY-MM-DD')); console.log(moment().format('MM/DD/YYYY')); console.log(moment().format('DD-MM-YYYY'));
Top answer
1 of 3
2

If you don't see it in the documentation (and I don't either), that suggests that no, there is no current preset for that in Moment.

You could always add your own:

Object.defineProperty(moment.fn, "myYMD", { // or moment.prototype, they refer to
                                            // the same object according to the docs
    value: function() {
        return this.format("YYYY-MM-DD");
    },
    configurable: true,
    writable: true
});

(Using Object.defineProperty because I never add enumerable properties to prototypes.)

Note the prefix on the method name, to avoid conflicts with future extensions to Moment.

Example:

Object.defineProperty(moment.fn, "myYMD", {
    value: function() {
        return this.format("YYYY-MM-DD");
    },
    configurable: true,
    writable: true
});

// using it
console.log(moment().myYMD());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Or just use a utility method you pass the Moment instance into.

2 of 3
1

As version 2.20.0, you can use Special Formats (even if they are mentioned in the parsing section of the docs), for example you can use moment.HTML5_FMT.DATE to get output in YYYY-MM-DD format:

console.log(moment().format(moment.HTML5_FMT.DATE));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

If you want, you can also customize default format, to make format() (without parameter) display result in your preferred format:

console.log(moment().format());
moment.defaultFormat = 'YYYY-MM-DD';
console.log(moment().format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Or if you want you can extend moment using moment.fn and build a function that accepts your tokens (like 'C' and 'I'):

moment.fn.myFormat = function(token){
  if( token == 'C' ){
    return this.format('YYYY-MM-DD');
  } else if( token == 'I' ){
    return this.toISOString();
  } else {
    return this.format();
  }
}
console.log(moment().myFormat());
console.log(moment().myFormat('C'));
console.log(moment().myFormat('I'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

The Moment prototype is exposed through moment.fn. If you want to add your own functions, that is where you would put them.

🌐
Moment.js
momentjs.com › guides
Moment.js | Guides
moment('01/12/2016', 'DD/MM/YYYY', true).format() "2016-12-01T00:00:00-06:00"
🌐
The Code Barbarian
thecodebarbarian.com › formatting-javascript-dates-with-moment-js.html
Formatting JavaScript Dates with Moment.js | www.thecodebarbarian.com
const moment = require('moment'); const d = new Date('2019/06/01'); moment(d).format('MMMM d, YYYY'); // June 1, 2019 · The format() function takes in a string and replaces all instances of tokens with the corresponding date value.
🌐
freeCodeCamp
forum.freecodecamp.org › t › date-time-format-in-moment-js › 362075
Date time format in moment js - The freeCodeCamp Forum
March 22, 2020 - Hello friends How to display a date and time from input like this 2013-02-08 09:30 or 2013/02/08 09:30 by using moment js in react js. I am searching on www.moment.js but i didn’t find. Can anyone give me a suggestio…
🌐
TutorialsPoint
tutorialspoint.com › momentjs › momentjs_format.htm
MomentJS - Format
var changeddate = moment().format("[Today's Date is ] D MMM YYYY");