2012-07-10 18 views
0

通過使用日期對象我們如何計算重疊週數?如何計算Flex中的重疊週數?

對於如:

7月31日(星期二)是本週38號結束,不過本週38號星期日結束即月4

然而幾個月不同

這個任何想法

感謝所有

+0

我不確定你想要計算重疊週數。你只是想給一個約會嗎?然後「輸出」一週的最後一天?(星期六?) – JeffryHouser 2012-07-10 16:34:32

+0

不僅僅是日期,需要顯示一週的數字以及一個月的日期。例如,7月29日至7月31日的第38周是2012年7月的月份。然而,2012年8月1日到8月4日也進入同一周的數字,即38。所以這些日期是重疊的,因爲它們屬於2個不同的月份 – flex 2012-07-10 19:49:01

+0

我修改了我的答案,有關如何獲得weekOfYear,該周的第一個日期和這一週的最後一天 – JeffryHouser 2012-07-10 20:22:09

回答

1

在我們的日曆組成部分,我們使用此代碼來計算在一週的第一天;我敢打賭它可能會被修改以查找本週的最後日期。它利用的DateUtils library

public static const DAY_OF_MONTH:String = "date"; 

    /** 
    * This method gets the first date of the week which the given date is in. 
    * 
    * @param date This is the date for which we want to process. 
    * @param firstDayOfWeek The first day of the week, 0 (Sunday) - 6 (Saturday); 0 is the default. It will probably be used primarily for localization purposes. 
    * 
    * @return  This returns a date representing the first day of the week. 
    */ 
    public static function firstDateOfWeek(date:Date, firstDayOfWeek : int = 0):Date { 
     var dayIncrement : int = dayOfWeekLocalized(date, firstDayOfWeek); 

     var returnDate : Date = DateUtils.dateAdd(DateUtils.DAY_OF_MONTH,-dayIncrement,date); 
     return returnDate; 

    } 

    /** 
    * This method returns the position of the day in a week, with respect to the firstDayOfWeek localization variable. 
    * 
    * If firstDayOfWeek is 0; then the week is display 0 (Sunday), 1 (Monday), 2 (Tuesday), 3 (Wednesday), 4 (Thursday), 5 (Friday), 6 (Saturday). 
    * So, a Sunday would return 0, a Saturday would return 6, and so on. 
    * 
    * If firstDayOfWeek is 1; then the week is displayed as 0 (Monday), 1 (Tuesday), 2 (Wednesday), 3 (Thursday), 4 (Friday), 5 (Saturday), 6 (Sunday). 
    * However, this situation will not change the date.day value. For display purposes we need a Sunday to return 6, a Saturday to return 5, and so on. 
    * 
    * This will presumably be used for display purposes. 
    * 
    * @param date This is the date to process. 
    * @param firstDayOfWeek The first day of the week, 0 (Sunday) - 6 (Saturday); 0 is the default. It will probably be used primarily for localization purposes. 
    * 
    * @return  This returns a date representing the day’s location on the localized week display. 
    */ 
    public static function dayOfWeekLocalized(date:Date, firstDayOfWeek : int = 0):int { 
     var result : int = date.day - firstDayOfWeek; 
     if(result < 0){ 
      result += 7; 
     } 

     return result; 

    } 

要找到一個星期的最後一天,我懷疑你可以調用firstDateOfWeek和加6天數:

public static function lastDateOfWeek(date:Date, firstDayOfWeek : int = 0):Date { 
     var firstDateOfWeek : Date = firstDateOfWeek(date, firstDayOfWeek); 

     var returnDate : Date = DateUtils.dateAdd(DateUtils.DAY_OF_MONTH,6,firstDateOfWeek); 
     return returnDate; 
    } 

注:第二批代碼爲用瀏覽器編寫,完全沒有經過測試。


更新: 給出一個具體的日期,你可以找到使用在DateUtils library的WEEKOFYEAR方法WEEKOFYEAR數。使用上述方法來找到周的第一個和最後一個日期問題

概念是這樣的:

var weekOfYear : Number = DateUtils.weekOfYear(myDate); 
var firstDayOfWeek : Date = firstDateOfWeek(myDate); 
var lastDayOfWeek : Date = lastDateOfWeek(myDate); 
0

this blog post處理過這個問題。這很簡單,真的。任何給定月份的最後一天比下個月的第一天少一天。如果最後一天不是星期六,則會有重疊。

+0

原始海報想知道這個星期是否跨越了兩個月嗎?還是他想知道一週的實際最後一天是什麼?我還不清楚,這將回答第一個問題 – JeffryHouser 2012-07-10 17:39:10

+1

問題的措辭使人很難知道...... – 2012-07-11 00:52:04