在Crystal Reports XI中更正ISO周编号

时间:2012-12-04 10:41:40

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

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

1 个答案:

答案 0 :(得分:5)

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