Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <h1>MomentJS Date Validation Demo</h1>
<label for="start">Start Date:</label>
<input id="start" name="start" type="date" placeholder="yyyy-mm-dd" value="">

<label for="end">End Date:</label>
<input id="end" name="end" type="date" placeholder="yyyy-mm-dd" value="">

<div id="out1"></div>

<hr color=red />
<p>If you find this demo useful, please consider <a href="" target="_blank">donating $1 dollar</a> (secure PayPal link) for a coffee or purchasing one of my songs from <a href="" target="_blank"></a> or <a href="" target="_blank"></a> for only 0.99 cents each. </p>
<p>Rob uses and recommends <a href="" target="_blank">MochaHost</a>, which provides Web Hosting for as low as $1.95 per month, as well as unlimited emails and disk space!</p>




                Date.getFormattedDateDiff = function(start, end) {
  var intervals = ['years','months','weeks','days'],
      out = [];
  if (!moment.isMoment(start)) start = moment(start);
  if (!moment.isMoment(end))   end   = moment(end);
  for(var i=0; i<intervals.length; i++){
      var diff = end.diff(start, intervals[i]);
      start.add(diff, intervals[i]);
      out.push(diff + ' ' + intervals[i]);
  return out.join(', ');

function calculateInterval() {
   var start      = document.getElementById('start'),
       end        = document.getElementById('end'),
       datesInfo  = validateDates({
         val: start.value
        }, {
         val: end.value
       out        = '';
   start = moment(start.value);
   end   = moment(end.value);
   if ( datesInfo === true ) {
     out = 'Time elapsed between "' + start.toISOString().split('T')[0]
         + '" and "' + end.toISOString().split('T')[0] + '":<br/>' 
         + Date.getFormattedDateDiff(start, end);
   else {
     out = datesInfo.msgs.join('<br/>');
     with (document.getElementById(datesInfo.eltId)) {
   return out;

function validateDates() {
  var dates     =,
      errs      = {
        eltId: '',
        msgs:  []
      dateParts = ['year',
      isValid = dates.every(function(date) {
        var aDate = moment(date.val, 'YYYY-MM-DD', true);

        if ( aDate.isValid() ) { 
          return true; 
        else { //get more info
          errs.eltId        =;
          errs.msgs[0]     = "There were errors in the date '" + date.val + "':";
          var parsingFlags = aDate.parsingFlags();

          //overflow: An overflow of a date field, such as a 13th month, a 32nd day of the month 
          //(or a 29th of February on non-leap years), a 367th day of the year, etc. 
          //overflow contains the index of the invalid unit to match #invalidAt 
          //-1 means no overflow.
          if (parsingFlags.overflow > -1)
            errs.msgs.push('Invalid ' + dateParts[parsingFlags.overflow] + '.');
          //invalidMonth: An invalid month name, such as moment('Marbruary', 'MMMM');. 
          //Contains the invalid month string itself, or else null.
          if (parsingFlags.invalidMonth)
            errs.msgs.push("'" + parsingFlags.invalidMonth + "' is an invalid month name.");
          //empty: An input string that contains nothing parsable,
          //such as moment('this is nonsense');. Boolean.   
          if (parsingFlags.empty)
            errs.msgs.push("A required date was missing or unparsable.");
          //nullInput: A null input, like moment(null);. Boolean.
          if (parsingFlags.nullInput)
            errs.msgs.push("You supplied a null value.");
          //invalidFormat: An empty list of formats, such as moment('2013-05-25', []). Boolean.
          if (parsingFlags.invalidFormat)
            errs.msgs.push("The following formats were empty: " + parsingFlags.invalidFormat);
          //userInvalidated: A date created explicitly as invalid, 
          //such as moment.invalid(). Boolean.
          if (parsingFlags.userInvalidated)
            errs.msgs.push("A date was created explicitly as invalid.");
          //unusedTokens: array of format substrings not found in the input string
          if (parsingFlags.unusedTokens.length)
            errs.msgs.push("The following date parts were not found in the input string: " + parsingFlags.unusedTokens);
          //unusedInput: array of input substrings not matched to the format string
          if (parsingFlags.unusedInput.length)
            errs.msgs.push("The following input was not utilized: " + parsingFlags.unusedInput); 

          return false;
  return isValid || errs;

function testDates(startDate, endDate) {
  var p = document.createElement('p');
  start.value = startDate;
  end.value   = endDate;
  p.innerHTML = '<strong>Testing "' + startDate + '" and "' 
              + endDate + '</strong>":<br/>' + calculateInterval();

//test each scenario
var start   = document.getElementById('start'),
    end     = document.getElementById('end'),
    out     = document.getElementById('out1'),
    today   = moment(new Date()).format('YYYY-MM-DD'),
    newYear = moment(new Date((new Date()).getFullYear(),0, 1)).format('YYYY-MM-DD'),
    y2k     = moment(new Date(2000, 0, 1)).format('YYYY-MM-DD');

//2 valid dates
testDates(newYear, today);

testDates(newYear, '2017-02-30');

testDates(newYear, 'bad date!');