6 Replies Latest reply on Oct 20, 2007 12:54 AM by Giribabums

    How to generate number of dates for an year [sunday or monday or tuesday]

    Giribabums
      I want to generate number of holidays for 1 year. based on checkBox which i selected i want to generate all days within a year. example - if i select sunday & monday i want to generate all sunday,mondays [dates] for an year.

      number of sundays and mondays [dates] for an year
        • 1. Re: How to generate number of dates for an year [sunday or monday or tuesday]
          enfantterrible-tijzQ7 Level 1
          One way would be to get the milliseconds for january 1,xxxx with getMilliseconds function of the Date Class. then increment the day in milliseconds (60s * 60m * 24h *1000) with the setMiliseconds function (ie previousMilliseconds_var + oneDayMilliseconds_var). With every day increment you test if it's a sunday or a monday with getDay() 0=sunday 1 = monday.

          I suggest using milliseconds because Flex will take care of all the advancing of the day and month for you.

          • 2. Re: How to generate number of dates for an year [sunday or monday or tuesday]
            Giribabums Level 1
            I have written this code to find endDate
            [code]
            public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;
            private function GenerateNoofHolidays():void
            {
            var myDate:Date = new Date();
            var startDate:Date = new Date(myDate.fullYear, myDate.month, myDate.date, 0, 0, 0, 0);
            var endDate:Date = new Date(startDate.getTime() + (365 * millisecondsPerDay));

            Alert.show(startDate +"\n "+ endDate);
            }
            [/code]

            in my database i am setting the standard working week like this
            sun = false
            mon = true
            tue = true
            wed = true
            thu = true
            fri = true
            sat = false
            and storing the values in array. From that array i can find which day is holiday and working day. [false represents holiday and true working day].
            From above code i am able to find endDate and startDate is currentDate.

            my question is from above array i got [sunday and saturday as holiday] so i want to retrieve or generate all dates sundays and saturdays from startDate to endDate
            • 3. Re: How to generate number of dates for an year [sunday or monday or tuesday]
              enfantterrible-tijzQ7 Level 1
              I think this code will work, you could I guess challenge the array by looping it before incrementing the day, but why?


              public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;

              private function GenerateNoofHolidays():void


              {

              var myDate:Date = new Date();
              var startDate:Date = new Date(myDate.fullYear, myDate.month, myDate.date, 0, 0, 0, 0);
              var endDate:Date = new Date(startDate.getTime() + (365 * millisecondsPerDay));

              While(!startDate > endDate){
              if(startDate.getDay() == 0){
              day is Sunday and do something ie put full date of this day in an array / arraycollection for manipulation

              } else if(startDate.getDay() == 6){
              day is Suturday and do something ie put full date of this day in an array / arraycollection for manipulation
              }
              startDate.setMilliseconds(startDate.getMilliseconds() + millisecondsPerDay)
              //
              }

              Alert.show(startDate +"\n "+ endDate);
              }
              • 4. Re: How to generate number of dates for an year [sunday or monday or tuesday]
                Giribabums Level 1
                Thanks for your reply, My requirement is to set the number of work days for a company, i have placed set checkbox where the number of work days can be set for particular company. like if a company is working on Sunday - Thursday they can set the working days and Friday-Saturday is their holiday. And I want to show the list of holiday [fridays and saturday] in a listbox. so that they can change [ convert ] holiday as working day by selecting the date which they want to change as working day from the listbox.
                I hope you understood the requirement
                Thanks once again
                • 5. Re: How to generate number of dates for an year [sunday or monday or tuesday]
                  enfantterrible-tijzQ7 Level 1
                  What I would do is match an array or an arrayCollection index with Flex's number for the day of the week. For example index 0 would always represent Sunday.
                  The checkbox status would change the value of the array at that index, turn myArray[0] to either true or false

                  public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;

                  private var myArray:Array = new Array;
                  myArray[0] = false
                  myArray[1] = true
                  myArray[2] = true
                  myArray[3] = true
                  myArray[4] = true
                  myArray[5] = true
                  myArray[6] = false

                  private function GenerateNoofHolidays():void
                  {

                  var myDate:Date = new Date();
                  var startDate:Date = new Date(myDate.fullYear, myDate.month, myDate.date, 0, 0, 0, 0);
                  var endDate:Date = new Date(startDate.getTime() + (365 * millisecondsPerDay));

                  While(!startDate > endDate){
                  for(var x:Number = 0; x < myArray.length; x++){
                  if(myArray[startDate.getDay()]== false){
                  it's a holiday day do something ie put full date of this day in an array / arraycollection for manipulation

                  }
                  startDate.setMilliseconds(startDate.getMilliseconds() + millisecondsPerDay)
                  }
                  //
                  }

                  Alert.show(startDate +"\n "+ endDate);
                  }
                  • 6. Re: How to generate number of dates for an year [sunday or monday or tuesday]
                    Giribabums Level 1
                    Thanks for your reply :)
                    Here the full code
                    [code]
                    public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;

                    private function GenerateNoofHolidays():void
                    {
                    CursorManager.setBusyCursor();
                    var myArray:Array = new Array();
                    myArray[0] = arrWorkingDays[0].Sun.toString();
                    myArray[1] = arrWorkingDays[0].Mon.toString();
                    myArray[2] = arrWorkingDays[0].Tue.toString();
                    myArray[3] = arrWorkingDays[0].Wed.toString();
                    myArray[4] = arrWorkingDays[0].Thu.toString();
                    myArray[5] = arrWorkingDays[0].Fri.toString();
                    myArray[6] = arrWorkingDays[0].Sat.toString();

                    var myDate:Date = new Date();
                    var startDate:Date = new Date(myDate.fullYear, myDate.month, myDate.date, 0, 0, 0, 0);
                    var endDate:Date = new Date(startDate.getTime() + (365 * millisecondsPerDay));

                    while(startDate < endDate)
                    {
                    for(var i:int = 0; i <myArray.length; i++)
                    {
                    var j:int = startDate.getDay();
                    if(myArray[j] == "false")
                    {
                    arrList.push(myDateFormat(startDate,"MM/DD/YYYY"));
                    }
                    startDate.setMilliseconds(startDate.getMilliseconds() + millisecondsPerDay);
                    }
                    }
                    lstHolidays.dataProvider = arrList;
                    CursorManager.removeBusyCursor();
                    }

                    // Return the date in specified or given format [ MM/DD/YYYY, DD/MM/YYYY, MM-DD-YYYY etc., ]
                    private function myDateFormat(date:Date,strDateFormat:String):String
                    {
                    var fDate:String;
                    var mydf:DateFormatter = new DateFormatter();
                    mydf.formatString = strDateFormat;
                    return fDate = mydf.format(date);
                    }
                    [/code]