簡體   English   中英

如何在flex中的datechooser中獲取星期六列和星期日列的日期數組

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

在datechooser組件中,我想獲取一個月中特定列的所有日期值,例如:星期六在數組中出現的所有日期值。 任何人都可以幫助我如何在數組中獲取這些列值。

謝謝

我知道你是從DateChooser那里說的,但是如果沒有其他人提出帶有dateChooser的解決方案,作為后退你可以通過以下方式獲得你想要的AS的Date對象:

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

坦率地說,編寫一個不使用DateChooser的util函數會更容易

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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM