Forum Discussion
Hi Yaseen
This will do it:
public string GetPreviousQuarter(string time)
{
var parts = time.Split('M');
int year = 0;
int month = 0;
if (int.TryParse(parts[0], out year) && int.TryParse(parts[1], out month))
{
// Determine the current quarter
int currentQuarter = (month - 1) / 3 + 1;
// Calculate the previous quarter
int previousQuarter = currentQuarter - 1;
// If the current quarter is Q1, then the previous quarter is Q4 of the previous year
if (previousQuarter < 1)
{
previousQuarter = 4;
year -= 1; // Decrement the year as we're moving to the previous year
}
return $"{year}Q{previousQuarter}";
}
else
{
return $"{time} is not a valid time";
}
}
Public Function GetPreviousQuarter(time As String) As String
Dim parts() As String = time.Split("M"c)
Dim year As Integer = 0
Dim month As Integer = 0
If Integer.TryParse(parts(0), year) AndAlso Integer.TryParse(parts(1), month) Then
' Determine the current quarter
Dim currentQuarter As Integer = (month - 1) \ 3 + 1
' Calculate the previous quarter
Dim previousQuarter As Integer = currentQuarter - 1
' If the current quarter is Q1, then the previous quarter is Q4 of the previous year
If previousQuarter < 1 Then
previousQuarter = 4
year -= 1 ' Decrement the year as we're moving to the previous year
End If
Return $"{year}Q{previousQuarter}"
Else
Return $"{time} is not a valid time"
End If
End Function
Call it using standard OS time like GetPreviousQuarter("2024M4") will return 2024Q1`
Related Content
- 11 months ago
- 4 months ago
- 11 months ago
- 9 months ago