如何获取Crystal Report中的每周或7天的日期范围

时间:2014-04-22 06:32:45

标签: crystal-reports crystal-reports-2008

如何获取7天或每周取决于Crystal Report的公式字段中的日期范围?

Example: 
   Date Range from March 01, 2014 to March 31, 2014 

Output:
    Week 1 (March 01 to March 07) 
    Week 2 (March 08 to March 14) 
    Week 3 (March 15 to March 21) 
    Week 4 (March 22 to March 28)          
    Week 5 (March 29 to March 31)

2 个答案:

答案 0 :(得分:0)

自定义功能RangeWeekSplitter可在一年内使用。 Crystal语法。

Function  (dateTimeVar dFrom, dateTimeVar dTo)

// First day of the year
local dateTimeVar dBegCurrYear:= Date (Year(dFrom), 1, 1);

// Day of year (1 to 365 or 366 in a leap year)
local numberVar nFrom:= DatePart ("y", dFrom);
local numberVar nTo:= DatePart ("y", dTo);

local numberVar i;
local numberVar iTo;
local numberVar nDaysInWeek:= 7; // number of days in the week
local numberVar nWeek:= 0; // counter weeks
local stringVar sResult:= ""; // output string

for i:= nFrom to nTo step nDaysInWeek do
(
    nWeek:= nWeek+1;
    iTo:= i+(nDaysInWeek-1);

    if(i+nDaysInWeek > nTo)
        then iTo:= nTo;

    // generate output string
    sResult:= sResult + chr(13)+
        "Week " + CStr(nWeek) + " (" +
            CStr(DateAdd ("y", i-1, dBegCurrYear), "MMMM d") +
                " to " +
            CStr(DateAdd ("y", iTo-1, dBegCurrYear), "MMMM d") +
        ")";
);

sResult;

用法示例:

// Date range
local dateTimeVar dFrom:= Date (2016, 1, 14);
local dateTimeVar dTo:= Date (2016, 3, 4);

RangeWeekSplitter (dFrom, dTo);

答案 1 :(得分:0)

您也可以将DatePart"ww"一起使用。见IBM Knowledge Center

  

DatePart(intervalType,inputDateTime)

     

...

     

ww:一年中的一周(第1天到第53天,第一周和第一周确定一年中第一个日历周的确切天数)

结合获得星期几的DatePart ("w", inputDateTime)DayOfWeek(inputDateTime),您可以计算当前日历周的第一天和最后一天。

因此,对于一个特定日期(inputDateTime),这将是您的公式“RangeWeek”:

Function (DateTimeVar inputDateTime)
NumberVar cw := DatePart("ww", inputDateTime);
DateTimeVar first := DateAdd("d", 1 - DayOfWeek(inputDateTime, crMonday), inputDateTime);
DateTimeVar last := DateAdd("d", 7 - DayOfWeek(inputDateTime, crMonday), inputDateTime);

"Week " + ToText(cw) + " (" + ToText(first) + " to " + ToText(last) + ")"

当然,您需要为ToText提供所需的格式字符串。

示例:

Input:  "August 23, 2017"
Output: "Week 34 (August 21 to August 27)"

在您获得日期范围的公式中,这会使事情变得更容易。

DateTimeVar from := ...;
DateTimeVar to := ...;
NumberVar cw;
NumberVar count := 0;
StringVar output := "";
for cw := DatePart("ww", from) to DatePart("ww", to) do
(
    output := output + chr(13) + RangeWeek(DateAdd("d", 7*count, from));
    count := count + 1;
);

output