0

通过使用日期对象,我们如何计算重叠周数?

例如:

7 月 31 日(星期二)是第 38 周的结束,但是第 38 周在星期日结束,即 8 月 4 日。

但是月份不同

对此有任何想法

谢谢大家

4

2 回答 2

1

在我们的日历组件中,我们使用此代码计算一周中的第一个日期;我敢打赌它可以被修改以找到一周的最后一个日期。IT 利用DateUtils 库

    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 库中的 weekOfYear 方法找出 weekOfYear 数字。使用上述方法查找相关一周的第一个和最后一个日期

概念上是这样的:

var weekOfYear : Number = DateUtils.weekOfYear(myDate);
var firstDayOfWeek : Date = firstDateOfWeek(myDate);
var lastDayOfWeek : Date = lastDateOfWeek(myDate);
于 2012-07-10T16:43:52.867 回答
0

我在这篇博文中处理了这个问题。这很简单,真的。任何给定月份的最后一天比下个月的第一天少一天。如果最后一天不是星期六,则存在重叠。

于 2012-07-10T17:06:36.523 回答