Pen Settings

HTML

CSS

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

JavaScript

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

Behavior

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.

HTML

              
                <h1 class=c>On what day of the week were you born?</h1>

<div class=c>
<b><p>Please Enter your birthday</b> (then click the <b>&ldquo;Update&rdquo;</b> 
button)</p>  


<form onreset="messagesElt.innerHTML = '&nbsp;';">
  <p>Numeric Month (1-12): <input TYPE="number" NAME="Inputmonth" value="12">  
  &nbsp;&nbsp;Day of Month (1-31): <input TYPE="number" NAME="Inputday"value="27">     
  &nbsp;&nbsp;Year (e.g. 1960): <input TYPE="number" NAME="Inputyear" value="1990">   
  &nbsp;&nbsp;<input TYPE="button" VALUE="Update" ONCLICK="compute(this.form)"> 
	<input TYPE="reset" VALUE="Clear"> </p>
<p id="messages">&nbsp;</p>
<!-- Output Age in Years Months and Days Format -->
  <p>Today You Are: <input TYPE="text" NAME="AgeYMD" SIZE="58" readonly></p>
</div>


<p>&nbsp;</p>
<hr size="3" noshade color="#F00000">

<br />
<p>If you find this demo useful, please consider <a href="https://www.paypal.me/RobertGravelle/1" target="_blank">donating $1 dollar</a> for a coffee using the button below 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. I have also released CDs with my band <a href="https://www.cdbaby.com/Artist/IvoryKnight" target="_blank">Ivory Knight</a> and Annihilator main-man Jeff Waters.</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>
              
            
!

CSS

              
                body { background-color: lightgray; font-family: Arial, sans-serif; font-size: 16px; }

.c	{ text-align: center; }

h1 { 
  font-size: 1.5em !important; 
  font-style: italic; 
  color: #004080;
}

input[type="text"], input[type='number'], input[type="date"] { font-size:18px; }
input[type="number"] { width: 75px; }
input[readonly] { background-color: #dedede; border: none; }

#messages    { color: red; }
              
            
!

JS

              
                var messagesElt = document.getElementById('messages');

String.prototype.singularize = function(num) {
  return (num == 1 ? this.slice(0, -1) : this);
};

/*
Date.getFormattedDateDiff2 = function(date1, date2, intervals) {
  var b    = moment(date1),
      a    = moment(date2),
      dur  = moment.duration(a.diff(b)),
      out  = [];
 
  intervals.forEach(function(interval) {
    var intervalDuration = dur[interval]();
    //unlike the other getters for duration, weeks are counted
    //as a subset of the days, and are not taken off the days count.
    if (interval == 'days') intervalDuration %= 7;
    out.push(intervalDuration + ' ' +  interval.singularize(intervalDuration));
  });
  
  return out.join(', ');
};
*/
Date.getFormattedDateDiff = function(date1, date2, intervals) {
  var b = moment(date1),
      a = moment(date2),
      intervals = intervals || ['years','months','weeks','days'],
      out = [];

  intervals.forEach(function(interval) {
      var diff = a.diff(b, interval);
      b.add(diff, interval);
      out.push(diff + ' ' + interval.singularize(diff));
  });
  return out.join(', ');
};

function getBirthday(Inputyear, Inputmonth, Inputday) {
   var inputYear  = parseInt(Inputyear.value),
       inputMonth = parseInt(Inputmonth.value),
       inputDay   = parseInt(Inputday.value),
       birthday;
   
   if ( isNaN(inputDay) ) {
      messagesElt.innerText = 'Invalid day.';
      Inputday.focus();
   }
   else if ( isNaN(inputMonth) ) {
     messagesElt.innerText = 'Invalid month.';
     Inputmonth.focus();
   } 
   else if (inputMonth < 1 || inputMonth > 12) {
      messagesElt.innerText = "Month is out of range";
      Inputmonth.focus();
   } 
   else if ( isNaN(inputYear) ) {
     messagesElt.innerText = 'Invalid year.';
     Inputyear.focus();
   }
   else if (inputYear < 1900) {
      messagesElt.innerText = "You're that old!?";
      Inputyear.focus();
   }
   else {
     var tempBirthday = moment([inputYear, inputMonth-1, inputDay]);
     if ( tempBirthday.isValid() ) {
       if ( tempBirthday.diff(new Date(), 'days') < 0 ) {
         birthday = tempBirthday;
       }
       else {
         messagesElt.innerText = "Your birthdate must be in the past!";
         Inputday.focus();
       }
     }
     else if ( tempBirthday.invalidAt(2) ) {  //month overflow
        messagesElt.innerText = "Day is out of range";
        Inputday.focus();
     }
   }
   
   return birthday;
}

function compute(form) {
   messagesElt.innerHTML = '&nbsp;';
  
   var today       = moment(new Date()),
       birthday    = getBirthday(form.Inputyear, form.Inputmonth, form.Inputday);
   
   if (!birthday) return;
   
   form.AgeYMD.value   = Date.getFormattedDateDiff( birthday, today );
   
}
              
            
!
999px

Console