cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation


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.

              <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="https://paypal.me/RobertGravelle/1" target="_blank">donating $1 dollar</a> (secure PayPal link) for a coffee or purchasing one of my songs from <a href="https://ax.itunes.apple.com/WebObjects/MZSearch.woa/wa/search?term=rob%20gravelle" target="_blank">iTunes.com</a> or <a href="http://www.amazon.com/s/ref=ntt_srch_drd_B001ES9TTK?ie=UTF8&field-keywords=Rob%20Gravelle&index=digital-music&search-type=ss" target="_blank">Amazon.com</a> for only 0.99 cents each. </p>
<p>Rob uses and recommends <a href="http://www.mochahost.com/2425.html" 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({
         id:  start.id,
         val: start.value
        }, {
         id:  end.id,
         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     = Array.prototype.slice.call(arguments),
      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        = date.id;
          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!');
Loading ..................