3 Replies Latest reply on Nov 10, 2017 6:22 AM by kglad

    How can I get  previous 2 day and next 2 day on calendar.

    KucuKeko Level 1

      I know today " currDateTime.date ".  I need to 2 previous and 2 next day on calendar for use this information on another mc.

       

      Example:                  

       

      previous 2previous1TodayNext 1
      Next2
      2829123

       

       

       

      package  {

       

          import flash.display.Sprite;

          import flash.text.TextFormat;

          import flash.text.TextField;

         

          import flash.events.Event;

          import fl.controls.ComboBox;

          import fl.data.DataProvider;

          import fl.controls.NumericStepper;

         

          public class Calendar extends Sprite {

         

              //variables

              private var cellW:Number; //cell width

              private var cellP:Number; //cell padding

              private var allDatesCells:Array = new Array();

              private var dateCellFormat:TextFormat;

             

              private var weekDays:Array = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

              private var dayLabelTxtFmt:TextFormat;

                     

              private var currDateTime:Date = new Date();

              private var firstDay:Date = new Date(currDateTime.fullYear,currDateTime.month,1);

              private var firstDayColumn:uint = firstDay.day;

              private var daysOfMonths:Array = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

              private var maxDays:uint;

             

              private var months:Array = [

                                          {label:"January", data:0},

                                          {label:"February", data:1},

                                          {label:"March", data:2},

                                          {label:"April", data:3},

                                          {label:"May", data:4},

                                          {label:"June", data:5},

                                          {label:"July", data:6},

                                          {label:"August", data:7},

                                          {label:"September", data:8},

                                          {label:"October", data:9},

                                          {label:"November", data:10},

                                          {label:"December", data:11},

                                         ];

       

              private var monthPickerCB:ComboBox; //combobox to pick a month

              private var yearPickerNS:NumericStepper; //numeric stepper to pick a year

             

             

              public function Calendar( fontFace:String = "Arial", fontSize:int = 15,

                                        cellWidth:Number = 30, padding:Number = 3,

                                        originX:Number = 15, originY:Number = 15,

                                        cbX:Number = 15, cbY:Number = 15,

                                        nsX:Number = 26, nsY:Number = 15,

                                        monthsRange:int = 39 )

              {

       

                  cellW = cellWidth;

                  cellP = padding;

             

                  monthPickerCB = new ComboBox();

                 

                  yearPickerNS = new NumericStepper();

             

                  setTextFormat( fontFace, fontSize );

             

                  makeDatesCellGrid( originX, originY );

             

                  makeDaysLabels( originX, originY );

             

                  monthSetup();

             

                  monthPicker( cbX, cbY );

                 

                  yearPicker( nsX, nsY, monthsRange );

             

              }

             

              private function setTextFormat(whichFont:String, size:int):void    {

                 

                  //date text format

                  dateCellFormat = new TextFormat();

                  dateCellFormat.font = whichFont;

                  dateCellFormat.color = 0xFFFFFF;

                  dateCellFormat.size = size;

                  dateCellFormat.align = "center";

       

                  //day label text format

                  dayLabelTxtFmt = new TextFormat();

                  dayLabelTxtFmt.font = "_sans";

                  dayLabelTxtFmt.color = 0x000000;

                  dayLabelTxtFmt.size = size - 3;   

              }

             

              private function makeDatesCellGrid(cellXPos:Number, cellYPos:Number):void {

       

                  //Create grid of date cells

                  for (var i:int = 0; i < 42; i++) {

       

                      var dateCell:TextField = new TextField();

                      addChild(dateCell);

                     

                      //position cells to form a grid (7 x 6 = 42)

                      dateCell.x = cellXPos + (cellW * (i-(Math.floor(i/7)*7)));

                      dateCell.y = cellYPos + (cellW * Math.floor(i/7));

       

                      //put all date cells into array for further access

                      allDatesCells.push(dateCell);

       

                  }

              }

             

              private function makeDaysLabels(cellXPos:Number, cellYPos:Number):void {

                 

                  //Add week day names

                  for (var i:int = 0; i < 7; i++)    {

       

                      var dayLabel:TextField = new TextField();

                      addChild(dayLabel);

       

                      dayLabel.selectable = false;

                      dayLabel.text = weekDays[i];

                      dayLabel.setTextFormat(dayLabelTxtFmt);

                      dayLabel.x = cellXPos + (cellW * i);

                      dayLabel.y = cellYPos - 15;

                  }

              }

             

              private function monthSetup():void {

                 

                  for (var i:int = 0; i < 42; i++){

                                 

                      allDatesCells[i].text = "";

                     

                      //decor all cells

                      allDatesCells[i].background = true;

                      allDatesCells[i].backgroundColor = 0x005555;

                      allDatesCells[i].border = true;

                      allDatesCells[i].borderColor = 0xCCCCCC;

                      allDatesCells[i].selectable = false;

                      allDatesCells[i].width = allDatesCells[i].height = cellW - cellP;

                      allDatesCells[i].setTextFormat(dateCellFormat);

                  }

                 

                  arrangeDates();

                  prevMonthDates();

                  nextMonthDates();

              }

             

              private function arrangeDates():void {

                 

                  //get column number for first day of the month

                  if (firstDay.day == 0)

                  {

                      //when last date of previous month is on saturday then move to second row

                      firstDayColumn = firstDay.day + 7;

                  }

                  else

                  {

                      firstDayColumn = firstDay.day;

                  }

                 

                  //get max days for current month w.r.t leap year if any

                  maxDays = (firstDay.getFullYear()%4 == 0 && firstDay.getMonth() == 1 ? 29 : daysOfMonths[firstDay.getMonth()]);

                 

                  //put dates for current month

                  for (var i:int = 0; i < maxDays; i++) {

             

                      allDatesCells[firstDayColumn + i].text = i + 1;

                      allDatesCells[firstDayColumn + i].setTextFormat(dateCellFormat);

             

                      allDatesCells[firstDayColumn + i].alpha = 1;

             

                      //Highlight today

                      if (firstDay.fullYear == currDateTime.fullYear && firstDay.month == currDateTime.month)

                      {                   

                          if(allDatesCells[firstDayColumn + i].text == currDateTime.date)

                          {

                              allDatesCells[firstDayColumn + i].backgroundColor = 0xEE5D15;

                              trace(currDateTime.date +i);

                          }

                      }

                  }

              }

             

              private function prevMonthDates():void {

       

                  var prevMonthFirstDay:Date = new Date(firstDay.fullYear,firstDay.month,firstDay.date - 1);

       

                  for (var i:int = firstDayColumn-1; i >= 0; i--) {

                     

                      allDatesCells[i].text = prevMonthFirstDay.date - ((firstDayColumn - 1) - i);

                      allDatesCells[i].setTextFormat(dateCellFormat);

                      allDatesCells[i].alpha = 0.5;

                  }

              }

             

              private function nextMonthDates():void {

                 

                  for (var i:int = 1; i < (42 - maxDays - (firstDayColumn - 1)); i++){

                     

                      allDatesCells[(firstDayColumn - 1) + i + maxDays].text = i;

                      allDatesCells[(firstDayColumn - 1) + i + maxDays].setTextFormat(dateCellFormat);

                      allDatesCells[(firstDayColumn - 1) + i + maxDays].alpha = 0.5;

                  }

              }

             

              private function monthPicker(cbX:Number, cbY:Number):void {

       

                  monthPickerCB.dataProvider = new DataProvider(months);

                  addChild(monthPickerCB);

             

                  //position combobox

                  monthPickerCB.x = cbX;

                  monthPickerCB.y = (cellW * 6) + cbY;

             

                  monthPickerCB.selectedIndex = currDateTime.month;

             

                  monthPickerCB.addEventListener(Event.CHANGE, pickMonth);

              }

       

              private function pickMonth(e:Event):void {

             

                  firstDay.month = ComboBox(e.target).selectedItem.data;

                  monthSetup();

              }

             

              private function yearPicker(nsX:Number, nsY:Number, maxYrsRange:int):void {

                 

                  yearPickerNS.maximum = currDateTime.fullYear + maxYrsRange;

                  yearPickerNS.minimum = currDateTime.fullYear - maxYrsRange;

                  yearPickerNS.value = currDateTime.fullYear;

                  addChild(yearPickerNS);

                 

                  //position numeric stepper

                  yearPickerNS.x = monthPickerCB.width + nsX;

                  yearPickerNS.y = (cellW * 6) + nsY;

       

                  yearPickerNS.addEventListener(Event.CHANGE, pickYear);

              }

       

              private function pickYear(e:Event):void    {

                 

                  firstDay.fullYear = e.target.value;

                  monthSetup();

              }

          }

      }

       

       

       

       

      Thanks for any help.

        • 1. Re: How can I get  previous 2 day and next 2 day on calendar.
          kglad Adobe Community Professional & MVP

          hi, use prevNextDate()

           

          var today:Date=new Date();

          var oneday:int = 24*60*60*1000;

           

          var prev1Day:Date=prevNextDate(-1);

          var prev2Day:Date=prevNextDate(-2);

          var next1Day:Date=prevNextDate(1);

          var next2Day:Date=prevNextDate(2);

          etc

           

           

          function prevNextDate(n:int):Date){

          return new Date(today.time-n*oneday);

          }

          • 2. Re: How can I get  previous 2 day and next 2 day on calendar.
            KucuKeko Level 1

            Thank you kglad I  use similar this

             

            Add in class file

             

            static public var binko:Number=0;//current day

                    static public var sinko:Number=0;//current day

                    static public var gecenson:Number=0;//end of the prev month

                    static public var buaysonu:Number=0;//end of the current month

                   

             

                   

            then use this code on frame

             

             

             

             

             

             

             

             

             

             

             

            if(Calendar.sinko==1){

                birsay.text=String(Calendar.gecenson-1)

                ikisay.text=String(Calendar.gecenson)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

            }else if(Calendar.sinko==2){

                birsay.text=String(Calendar.gecenson)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

            }else if(Calendar.sinko<27 && Calendar.binko>2){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

               

                //FEBRUARY IF 28

               

            }else if(Calendar.sinko==27 && Calendar.binko>2 && Calendar.buaysonu==28 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text="1";

               

            }else if(Calendar.sinko==28 && Calendar.binko>2 && Calendar.buaysonu==28 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text="1";

                bessay.text="2";

               

            }

             

             

                //FEBRUARY IF 29

            else if(Calendar.sinko==27 && Calendar.binko>2 && Calendar.buaysonu==29 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

                trace("29/1")

            }else if(Calendar.sinko==28 && Calendar.binko>2 && Calendar.buaysonu==29 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text="1";

                trace("29/2")

            }else if(Calendar.sinko==29 && Calendar.binko>2 && Calendar.buaysonu==29 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text="1";

                bessay.text="2";

                trace("29/3")

            }

             

             

             

             

             

            ///NORMAL 30

             

             

             

            else if(Calendar.sinko==27 && Calendar.binko>2 && Calendar.buaysonu==30 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

                trace("30/1")

            }else if(Calendar.sinko==28 && Calendar.binko>2 && Calendar.buaysonu==30 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

                trace("30/2")

            }else if(Calendar.sinko==29 && Calendar.binko>2 && Calendar.buaysonu==30 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text="1";

                trace("30/3")

            }else if(Calendar.sinko==30 && Calendar.binko>2 && Calendar.buaysonu==30 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text="1";

                bessay.text="2";

                trace("30/4")

            }

             

             

             

             

             

             

             

             

             

             

             

             

             

            ///NORMAL 31

             

             

             

            else if(Calendar.sinko==27 && Calendar.binko>2 && Calendar.buaysonu==31 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

                trace("31/1")

            }else if(Calendar.sinko==28 && Calendar.binko>2 && Calendar.buaysonu==31 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

                trace("31/2")

            }else if(Calendar.sinko==29 && Calendar.binko>2 && Calendar.buaysonu==31 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text=String(Calendar.sinko+2)

                trace("31/3")

            }else if(Calendar.sinko==30 && Calendar.binko>2 && Calendar.buaysonu==31 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text=String(Calendar.sinko+1)

                bessay.text="1";

                trace("31/4")

            }else if(Calendar.sinko==31 && Calendar.binko>2 && Calendar.buaysonu==31 ){

                birsay.text=String(Calendar.sinko-2)

                ikisay.text=String(Calendar.sinko-1)

                ucsay.text=String(Calendar.sinko)

                dortsay.text="1";

                bessay.text="2";

                trace("31/5")

            }