2012-12-04 45 views

回答

4

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

作爲反直覺因爲對我而言,2012年12月31日應該被視爲2013年第1周,看起來你是對的。這也是2008年的一個bug。 – Ryan

+0

如果你想每週都有7天的時間(你總是想要的那麼好),你必須決定什麼叫一週,跨越新的一年。稱它爲「2012-53/2013-01」可能會更直觀,但可以說並不是更好。 ISO-8601只是一週中大部分日子屬於哪一年的整個星期。 – LapplandsCohan

相關問題