Created
September 2, 2011 04:08
-
-
Save ScottPhillips/1187894 to your computer and use it in GitHub Desktop.
Find out how many years old someone is using jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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