• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 37. Date and Time > Finding How Long From Now Until Then

Finding How Long From Now Until Then

Here's some code to find the number of months and days between two dates:

// this assumes that the two events take place in the same year.
// Or, more specifically, now is earlier than October 19, 2004.
end_date = new Date(2004, 9, 19);
now_date = new Date();

var monthsLeft:Number;
var daysLeft:Number;

// find out how many months and days are left
// between now_date and end_date

// find months left
monthsLeft = end_date.getMonth() - now_date.getMonth();
if (now_date.getDate() > free_date.getDate())
{
      monthsLeft -= 1;
}

// find days left
if (now_date.getDate() > end_date.getDate())
{
      // get number of days from now until end of the month
      var daysInMonth:Number = new Date(now_date.getYear(),
      now_date.getMonth()+1, 0).getDate();
      var daysLeftInMonth:Number = daysInMonth - now_date.getDate()
      daysLeft = end_date.getDate() + daysLeftInMonth;

}
else
{
      daysLeft = end_date.getDate() - now_date.getDate();
}

trace("monthsLeft: " + monthsLeft);
trace("daysLeft: " + daysLeft);


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint