简体   繁体   中英

How to get array of dates for saturday column and sunday column in a datechooser in flex

In a datechooser component i want to get the all date values of a particular column in a month eg: All date values that occur in saturday in an array. Could any one help me how to get those column values in an array.

Thanks

I know you said from a DateChooser, but if no one else presents a solution with a dateChooser, as a fall back you can get what you want with AS's Date object by doing something like:

function getSaturdaysFor(year:Number, month:Number, dayOfWeek:Number):Array {
    var d:Date = new Date(year,month,1);
    var toReturn:Array = new Array();

    //Find the first occurance of the dayOfWeek
    for(var i:int = 0; i < 7; i++){
        if(d.day == dayOfWeek){ 
            break;
        } else {
            d.date++;
        }
    }

    //Find the remaning dayOfWeeks, skipping non-dayOfWeek dates
    for(var n:int = 0; n < 7; n++){ //Up to 6 possible
        if(d.month == month){
            trace("Added " + d.date);
            toReturn.push(d.date);
            d.date += 7;
        } else {
            break; //No more target days of week
        }
    }
    return toReturn;
}

//Call this function like
getSaturdaysFor(2012, 2, 6); //2012 March Saturdays
getSaturdaysFor(2012, 3, 0); //2012 April Sundays

Frankly, it would just be easier to write a util function which does that without using a DateChooser

function getDatesForDaysInMonth(year:Number, month:Number, dayOfWeek:Number):Array {
    var dts:Array=[];
    var dt:Date=new Date(year, month, 1);
    var firstInSeries:Date=new Date(dt);
    firstInSeries.date += (dayOfWeek-dt.day);
    if(firstInSeries.month != month) {
        firstInSeries.date += 7;
    }
    dts.push(firstInSeries);

    for(var i:int=1; true; i++) {
        var it:Date=new Date(firstInSeries.valueOf() + i*7*24*60*60*1000);
        if(it.month != month) {
            break;
        }
        dts.push(it);
    }
    return dts;
}

//tester function
function disp(a:Array):void {
    for(var i:int=0; i<a.length; i++) {
        trace(i + ": " + a[i].toString());
    }
    trace("------------------");
}

trace("Sat");
disp(getDatesForDaysInMonth(2012, 2, 6));
trace("Fri");
disp(getDatesForDaysInMonth(2012, 2, 5));
trace("Thu");
disp(getDatesForDaysInMonth(2012, 2, 4));
trace("Wed");
disp(getDatesForDaysInMonth(2012, 2, 3));
trace("Tue");
disp(getDatesForDaysInMonth(2012, 2, 2));
trace("Mon");
disp(getDatesForDaysInMonth(2012, 2, 1));
trace("Sun");
disp(getDatesForDaysInMonth(2012, 2, 0));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM