1 Reply Latest reply on Nov 7, 2009 6:17 PM by Subeesh Arakkan

    Date: getYesterday? get LastWeek?

    Yozef0 Level 1

      Èllow Chaps!


            I've really searched. I am creating a button that actually gives me yesterday's date (simply put solving this problem for General use) is to display Yesterday's date in this format: YYYY-MM-DD


      I will show you my code, and surely you'll notice this won't work, and is buggy, but I've tried:


      // Gets todays date and removes the days (offset)
      private function todayDay(offset:Number = 0):String {
           if ((_date.getDate()) < 10) {
                   // checking is < 10 to add a 0 for single digit days... example: November 1 = 11-01
                   trace("return: " + String("0" + Number(_date.getDate() + offset))); // Err: OFFSET may reduce the date to a negative day
                return String("0" + Number(_date.getDate() + offset)); 
                } else {
                          trace ("return 2: " + String(_date.getDate()));
                    return String(_date.getDate());


      An Example using this function would be like so:


      initialDate = String(_date.getFullYear()) + "-" + todayMonth() + "-" +  todayDay(-7);




      Now you could see the problem with this, is that if we are on the 4th of November, the date could be a - (minus) if the offset is -7 days.



      Is there a way I could create a function that returns me a date in the format YYYY-MM-DD by calling a function that gets me todays date and removes the offset days. (the months and day would also apply: ex: -10 days from the 2009-11-07 would return 2009-10-28

        • 1. Re: Date: getYesterday? get LastWeek?
          Subeesh Arakkan Level 4



          You can subtract 24 hours from the date object to get the previous day's date and subtract 24 * 7 to get the previous week's date.

          Once you get the new date, you can use DateFormatter to format it in any format.

          Check these methods

          private function getPreviousDay(date:Date):Date


               date.hours = date.hours - 24;

               return date;




          private function getPreviousWeek(date:Date):Date


               date.hours = date.hours - 24 * 7;

               return date;



          Using date formatter


          var df:DateFormatter = new DateFormatter();

          df.formatString = 'YYYY-MM-DD';

          var dateString:String = df.format(date);