在Crystal Reports中获取上一季度的开始和结束日期 [英] Get previous quarter start and end date in Crystal Reports
问题描述
像许多人一样(但不是水晶报告,我可以找到),我正在寻找如何获得上一季度的开始和结束日期。
Like many have asked (but not for Crystal Reports that I can find), I'm looking for how to get the previous quarter start and end dates.
由于今天是2014年7月10日,我可以使用
Since today is 7/10/2014, I can use
DateAdd("Q", -1, CurrentDate)
并返回4/10/2104 12:00:00 AM
and it returns 4/10/2104 12:00:00AM
我想要的是上一季度的开始和结束日期。对于今天,这将是4/1/2014和6/30/2014。在10月,它应该返回7/1/2014和9/30/2014。我只想要日期部分,而不是时间。
What I want is the previous quarter start and end dates. For today, that would be 4/1/2014 and 6/30/2014. In October, it should instead return 7/1/2014 and 9/30/2014. I only want the date part, not the time.
我通过水晶日期和时间和日期范围选项,只是没有看到我需要做的。
I'm looking through the Crystal Date and Time and Date Ranges options, and just not seeing what I need to do.
推荐答案
要获取季度的第一个日期,以下内容将有效:
To get the first date in the quarter, the following will work:
dateVar PrevQuarterDate := Date(DateAdd("Q", -1, CurrentDate));
dateVar PrevQuarterStart := Date(Year(PrevQuarterDate),Month(PrevQuarterDate),1);
PrevQuarterStart;
最后一个日期类似:
dateVar PrevQuarterDate := Date(DateAdd("Q", -1, CurrentDate));
dateVar PrevQuarterEnd := Date(Year(PrevQuarterDate),Month(PrevQuarterDate)+3,1)-1;
PrevQuarterEnd;
事实证明,如果上一季度也是 NOT 在上一年。以下是更好。
It turns out the above will NOT work if the previous quarter is also in a previous year. The following is better. There's enough code to put in your own date and display the results, so it can be checked, too.
dateVar ToDaysDate := CurrentDate; // Date(2014,02,11);
dateVar PrevQDtDate := Date(DateAdd("Q", -1, ToDaysDate));
dateVar PrevQStart := dateserial(year(PrevQDtDate),(datepart("q",DateAdd ("q", -1, ToDaysDate))*3)-2, 1);
DateVar PrevQEnd := dateserial(year(ToDaysDate),datepart('q',ToDaysDate)*3-2,1-1);
StringVar DispDates;
DispDates := totext(PrevQStart) + "-" + totext(PrevQEnd);
DispDates;
这篇关于在Crystal Reports中获取上一季度的开始和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!