Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created September 2, 2011 04:08
Show Gist options
  • Save ScottPhillips/1187894 to your computer and use it in GitHub Desktop.
Save ScottPhillips/1187894 to your computer and use it in GitHub Desktop.
Find out how many years old someone is using jQuery
Day: <input type="text" name="day" id="day" value ="5" />
<hr>
Month: <input type="text" name="month" id="month" value ="12" />
<hr>
Year: <input type="text" name="year" id="year" value ="1972" />
<hr>
Birthday: <span id="birthday">?</span>
<hr>
Today: <span id="todayIs">?</span>
<hr>
Years Old: <span id="yearsOld">?</span>
<hr>
<button id="submitForm">Calculate Age</button>
function checkAge() {
var d = $('#day').val();
var m = ($('#month').val() -1);
var y = $('#year').val();
var birthdate = new Date(y,m,d);
$('#birthday').html(birthdate.toString());
var today = new Date();
$('#todayIs').html(today.toString());
var diff = (today - birthdate);
var years = Math.floor(diff/(1000*60*60*24*365));
$('#yearsOld').html(years);
}
$(document).ready(function() {
$('#submitForm').click(checkAge);
});
name: Check How Many Years Old using jQuery
description: As a fiddle on jsFiddle: http://jsfiddle.net/gh/gist/jquery/1.6.2/1187894/?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment