Google help on software/programming And Windows Tricks: TO calculate the date difference in months and years

helponsoftware

Wednesday 7 August 2013

TO calculate the date difference in months and years

<input type="text" id="date1" value="08/2012">
<input type="text" id="date2" value="10/2012">
<button onclick="myFunction()">Try it</button>

<script>
function getDateObject(str) {
      var arr = str.split("/");
      return new Date(arr[1], arr[0]);
}
function myFunction()
{
var x = document.getElementById("date1").value;
date1=getDateObject(x);
var y = document.getElementById("date2").value;
date2=getDateObject(y);
var month = parseInt((date2 - date1) /(86400000*30));
if(month>12)
{
var year=parseInt(month/12);
alert(year);
var months=month%12;
alert(months);
}
}
</script>

No comments:

Post a Comment