【问题标题】:How to calculate overlap week in Flex?如何计算 Flex 中的重叠周?
【发布时间】:2012-07-10 16:30:24
【问题描述】:

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

例如:

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

但是月份不同

关于这个的任何想法

谢谢大家

【问题讨论】:

  • 我不确定您要计算重叠周数。你只是想约会吗?然后“输出”一周的最后一天?(星期六?)
  • 不仅是日期,还需要显示周数以及一个月中的日期。例如第 38 周,7 月 29 日至 7 月 31 日是 2012 年 7 月。但是 2012 年 8 月 1 日至 8 月 4 日也属于同一周数,即 38。所以这些日期是重叠的,因为它们属于 2 个不同的月份
  • 我修改了答案,提供了有关如何获取 weekOfYear、该周的第一个日期和该周的最后一个日期的信息。

标签: apache-flex actionscript flex4


【解决方案1】:

在我们的日历组件中,我们使用此代码计算一周中的第一个日期;我敢打赌它可以被修改以找到一周的最后一个日期。 IT 利用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);

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 原帖者想知道所讨论的一周是否跨越两个月?或者他想知道一周的最后一天是什么?我不清楚。这将回答第一个问题。
    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 2021-02-05
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    相关资源
    最近更新 更多