问题
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.
Since today is 7/10/2014, I can use
DateAdd("Q", -1, CurrentDate)
and it returns 4/10/2104 12:00:00AM
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.
回答1:
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;
And the last date is similar:
dateVar PrevQuarterDate := Date(DateAdd("Q", -1, CurrentDate));
dateVar PrevQuarterEnd := Date(Year(PrevQuarterDate),Month(PrevQuarterDate)+3,1)-1;
PrevQuarterEnd;
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;
回答2:
First date of previous quarter:
dateVar myDate := currentDate;
numberVar myMonth := Month(dateadd("q", -1, myDate));
numberVar myYear := Year(dateadd("q", -1, myDate));
myMonth := myMonth - ((myMonth + 2) MOD 3);
date(myYear, myMonth, 1);
Last date of pervious quarter:
dateVar myDate := currentDate;
numberVar myMonth := Month(myDate);
numberVar myYear := Year(myDate);
myMonth := myMonth - ((myMonth + 2) MOD 3);
date(dateadd("d", -1, date(myYear, myMonth, 1)));
来源:https://stackoverflow.com/questions/24687267/get-previous-quarter-start-and-end-date-in-crystal-reports