<div id="dateDisplay"></div>
<br>
<div id="parsedDateDisplay"></div>
<br>
<div id="isValid1"></div>
<br>
<div id="isValid2"></div>
<br>
<div id="parsedDate1"></div>
<br>
<div id="parsedDate2"></div>
<br>
<div id="formattedDate"></div>

//format date
document.getElementById("dateDisplay").innerHTML = moment().format('YYYY-MM-DD');

let date = moment('2017-04-21 09:30:26.123');
//parse date
document.getElementById("parsedDateDisplay").innerHTML = date;

//check if the date is valid
document.getElementById("isValid1").innerHTML = moment('2017-04-21 09:30:26.123').isValid();

document.getElementById("isValid2").innerHTML = moment('123456').isValid();

//parse to a specified format 
document.getElementById("parsedDate1").innerHTML = moment("04-21-2017", "MM-DD-YYYY");

document.getElementById("parsedDate2").innerHTML = moment("04/21/2017", "MM-DD-YYYY");

let formattedDate = moment('2017-04-21', ['MM-DD-YYYY', 'YYYY-MM-DD']);

document.getElementById("formattedDate").innerHTML = formattedDate;

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.0/moment.min.js