Skip to content

Instantly share code, notes, and snippets.

@shakirullahi
Last active July 15, 2016 19:05
Show Gist options
  • Save shakirullahi/509a388472a88e2a977d615508de4285 to your computer and use it in GitHub Desktop.
Save shakirullahi/509a388472a88e2a977d615508de4285 to your computer and use it in GitHub Desktop.
Experience calculator : create a html file and put this code and run
<input type="text" id="from" placeholder="Starting date dd-mm-yyyy">
<input type="text" id="to" placeholder="Ending date dd-mm-yyyy">
<button onclick="calculate()">Calculate</button>
<script>
function calculate(){
var from = document.getElementById('from').value;
var to = document.getElementById('to').value;
from = from.split('-');
var from_day = parseInt(from[0]);
var from_month = parseInt(from[1]);
var from_year = parseInt(from[2]);
to = to.split('-');
var to_day = parseInt(to[0]);
var to_month = parseInt(to[1]);
var to_year = parseInt(to[2]);
alert("Difference is " + Math.floor((g(to_year,to_month,to_day) - g(from_year,from_month,from_day))/365) +" years, "+Math.floor((g(to_year,to_month,to_day) - g(from_year,from_month,from_day))/30) + " months and " + Math.floor((g(to_year,to_month,to_day) - g(from_year,from_month,from_day)) % 30) + " " +" days")
}
function g(y,m,d){
m = (m + 9) % 12;
y = y - Math.floor(m/10);
return 365*y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) + Math.floor((m*306 + 5)/10) + ( d - 1 )
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment