【问题标题】:Correct ISO week numbering in Crystal Reports XICrystal Reports XI 中的正确 ISO 周编号
【发布时间】:2012-12-04 10:41:39
【问题描述】:

如何在 Crystal Reports XI 中获取给定日期的 ISO-8601 周数?

【问题讨论】:

    标签: crystal-reports iso crystal-reports-xi week-number


    【解决方案1】:

    Crystal Reports 支持DatePart-函数,它可以为您提供给定日期的 ISO 周数。

    NumberVar week := DatePart("ww", date, crMonday, crFirstFourDays);
    

    但是,Crystal Reports XI 中存在一个错误,它会在新年期间提供错误的结果。最好的解决方案可能是创建一个自己的函数 getISOWeekNumber:

    Function (optional DateVar d := CurrentDate)
    NumberVar week := DatePart("ww", d, crMonday, crFirstFourDays);
    
    // Correct for that CR doesn't handle the fact that the last days of a year can belong to week 1 of the next year:
    if week = 53 and DatePart("ww", cDate(year(d) + 1, 1, 1), crMonday, crFirstFourDays) = 1 then
        week := 1
    
    // A bug in CR makes DatePart return values like 9363 for days in January that belongs to the last week of the previous year.
    else if week > 53 then
        week := DatePart("ww", cDate(year(d) - 1, 12, 31), crMonday, crFirstFourDays);
    
    week;
    

    要获取特定日期的“周年”,您可以使用以下函数:

    // Returns the year to which the ISO week of the specified date belongs.
    // E.g. 2012-12-31 will return 2013, as that date belongs to week 1 of 2013.
    Function (optional DateVar d := CurrentDate)
    
    NumberVar week := getISOWeekNumber (d);
    
    if week = 1 and month(d) = 12 then
        year(d) + 1
    else if week > 10 and month(d) = 1 then
        year(d) - 1
    else
        year(d);
    

    【讨论】:

    • 2012 年 12 月 31 日应该被视为 2013 年的第 1 周,这对我来说是违反直觉的,看起来你是对的。这也是 2008 的一个错误。
    • 如果您希望每周有 7 天(您一如既往地希望如此),您必须决定将跨过新年的那一周称为什么。称它为“2012-53/2013-01”可能更直观,但可以说不是更好。 ISO-8601 只是简单地命名一周中的大多数日子属于哪一年之后的整周。
    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多