Tuesday, 5 July 2011

Calculate DateDiff using JavaScript

This script will let you know how long you have to wait for the New Year

<script type="text/javascript">

//Set the two dates
var today = new Date();

var endofyear =new Date(today.getFullYear(), 11, 31); //Month is 0-11 in JavaScript

if (today.getMonth()==11 && today.getDate()==31)
alert("Happy New Year " + today.getFullYear()+1);
else
{
//Set 1 day in milliseconds
var one_day=1000*60*60*24;

//Calculate difference btw the two dates, and convert to days
alert(Math.ceil((endofyear.getTime()-today.getTime())/(one_day))+
" days left until New Year!");
}
</script>

You might be also interested in DateAdd() function which can be used to add day/month/year to give date.

No comments:

Post a Comment