Get the date difference using Javascript

Madhu.B.Rokkam
Posted by Madhu.B.Rokkam under JavaScript category on | Points: 40 | Views : 1043
<script language = "javascript" type="text/javascript">

/* Javascript JavaScriptGetDateDiff function that return negative value if first date is greater that second*/
function JavaScriptGetDateDiff(fDate, sDate) {
var oneDay = 1000 * 60 * 60 * 24; //Convert into millisec
var firstDate = new Date(fDate);
var secondDate = new Date(sDate);
var noOfDays = Math.ceil((secondDate.getTime() - firstDate.getTime()) / oneDay);
if (!isNaN(noOfDays))
return noOfDays;
else
return 0;
}

alert(JavaScriptGetDateDiff('10/26/2012', '10/31/2012')); //returns 5
alert(JavaScriptGetDateDiff('10/31/2012','10/26/2012')); //returns -5

</script>

Comments or Responses

Login to post response