How to get start and end date of the previous quarter?

Hello there!

I was going through forum and found the below post:Referred post

1. Create a workflow with two arguments,
outPrevQuarterStartDate and outPrevQuarterEndDate with datetime datatype
2. Get previous quarter date based on current date
datePrevQuarterDate = DateTime.Now.AddMonths(-3)
3. Find previous quarter number
intPrevQuarterNumber = Convert.ToInt16((datePrevQuarterDate.Month - 1)/3+1)
4. Find start date of prev quarter
*outPrevQuarterStartDate = new DateTime(datePrevQuarterDate.Year, (intPrevQuarterNumber-1)3+1,1)
5. Find end date of prev quarter
outPrevQuarterEndDate = outPrevQuarterStartDate.AddMonths(3).AddDays(-1)

But then, this isn’t giving me the actual result.

For example: Today’s date: 08th Dec 2022 then the QNo is coming as 4 and this isn’t correct.

Can you help me with this?

Thanks in advance!!

Hi @smritijoshi

Can you let me know what are the quarters that you are considering accordingly we can provide a solution

cheers

Hi @Anil_G

I’m going to consider all the quarters. This is a quarterly usecase. So start and end date of the quarter is required. It can be this year or the last year as well.

For example: Start date and end date of last quarter for today’s date would be 01-Jul-2022 and 30-Sep-2022.

Start date and end date for 01-Jan-2023 would be 01-10-2022 and 31-12-2022.

I hope this is clear.

Test with the following code:

dt = Today.AddMonths(-3)
firstDayOfPrevQuarter = New DateTime(dt.Year, Convert.ToInt32(Math.Ceiling(dt.Month/3)*3-2), 1)
lastDayOfPrevQuarter = firstDayOfPrevQuarter.AddMonths(3).AddDays(-1)

1 Like

HI,

FYI, another approach

targetDate = now

Then

First day of previous quarter

New DateTime({0,-1,-1,-1,0,0,0,0,0,0,0,0,0}(targetDate.Month)+targetDate.Year, {0,10,10,10,1,1,1,4,4,4,7,7,7}(targetDate.Month),1)

Last dat of previous quarter

New DateTime(targetDate.Year, {0,1,1,1,4,4,4,7,7,7,10,10,10}(targetDate.Month),1).AddDays(-1)

Regards,

1 Like