Created
February 13, 2012 00:02
-
-
Save brunoborges/1811951 to your computer and use it in GitHub Desktop.
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
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script> | |
function showDiff() { | |
var s1 = $("#first").val(); | |
var s2 = $("#second").val(); | |
var d1 = toDate(s1); | |
var d2 = toDate(s2); | |
var diff = daydiff(d1, d2); | |
if (diff <= 0) { | |
diff = diff + " dia"; | |
} else { | |
diff = diff + " dias"; | |
} | |
$("#diff").text(diff); | |
} | |
function toDate(string) { | |
var date = new Date(); | |
var as = string.split("/"); | |
date.setDate(as[0]); | |
date.setMonth(as[1]); | |
date.setYear(as[2]); | |
return date; | |
} | |
function daydiff(first, second) { | |
return (second-first)/(1000*60*60*24) | |
} | |
$(document).ready(function(){ | |
$("#showDiff").click(showDiff); | |
}); | |
</script> | |
</head> | |
<body> | |
<div class="rowElem"> | |
<select name="first" class="first" id="first"> | |
<option value="17/02/2012">17/02/2012 (sexta-feira)</option> | |
<option value="18/02/2012">18/02/2012 (sábado)</option> | |
<option value="19/02/2012">19/02/2012 (domingo)</option> | |
</select> | |
</div> | |
<span class="text">e voltando em</span> | |
<div class="rowElem"> | |
<select name="second" class="second" id="second"> | |
<option value="20/02/2012">20/02/2012 (segunda-feira)</option> | |
<option value="21/02/2012">21/02/2012 (terça-feira)</option> | |
<option value="22/02/2012">22/02/2012 (quarta-feira)</option> | |
<option value="23/02/2012">23/02/2012 (quinta-feira)</option> | |
<option value="24/02/2012">24/02/2012 (sexta-feira)</option> | |
<option value="25/02/2012">25/02/2012 (sábado)</option> | |
<option value="26/02/2012">26/02/2012 (domingo)</option> | |
<option value="27/02/2012">27/02/2012 (segunda-feira)</option> | |
</select> | |
</div> | |
<input type="button" id="showDiff" value="Show diff" /> | |
<span id="diff"></span> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment