1 Reply Latest reply on Aug 20, 2010 10:29 AM by rtalton

    How can I add [# of days] to a date?

    EvolvedDSM Level 2

      edit::  Nevermind, I found the answer.  I'm using AS 2.0

      Answer: new Date(year, month, date+#)  where # is the # of days to add.

      Helpful link: http://blog.flexexamples.com/2007/08/24/date-math-for-lazy-people/  (AS 2.0 & 3.0)

       

      Original question:

       

      The title is horrible, couldn't think of how to word it.

       

      Anyway, I want to know how I can take a date, add a few days to it, then display the resulting date.

       

      For example, today's date is 8/20/2010.  I want to be able to add 10 days to this and then display the result in a label or other output.

       

      The label would then read 8/30/2010.  If I wanted to add 15 days to today's date, the label would read 9/4/2010.

       

      Any help is appreciated, thanks!

        • 1. Re: How can I add [# of days] to a date?
          rtalton Level 4

          Another way of adding values to a date object is to set up constants, and this will make life easier if you do Date math frequently, or are adding to a date which is unknown until runtime (i.e., not neccessarily today's date).

           

           

                      private var todayDate:Date = new Date();

           

                      //One day, in milliseconds:
                      private const oneDay:Number = 1000 * 60 * 60 * 24;

           

                      private function addDaysToTheDate():void {
                          //The number of days you wish to add:
                          var daysToAdd:Number = oneDay * 10;
                          //Add the days to today's date:
                          var answer:Date = new Date(todayDate.time + daysToAdd);
                          Alert.show(answer.toDateString());
                      }