Date Format +2 and last day

Hello, I am entering a date in SAP in one step. A format is requested in which the start date is the first day of the working month, the end date is +2 months and the last day of that month. The format I made adds two months but takes the day it works. I want to add +2 months and get the last day of that month. Can you help me?

for example = StartDate = 01/06/2023
end date = 31/08/2023 (last day of the month)

my codes are like this = StartDate = Datetime.now.ToString(“01.MM.yyyy”)
EndDate = Datetime.now.AddMonths(2).ToString(“dd.MM.yyyy”)

Hi @Ertan_Ay2

Please try the following expressions:

StartDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("dd/MM/yyyy")

EndDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month + 2, 1).AddDays(-1).ToString("dd/MM/yyyy")

Hope this helps,
Best Regards.

1 Like

Hi @Ertan_Ay2 ,

Try this.

EndDate = CDate(now.AddMonths(3).ToString(“MM.01.yyyy”)).adddays(-1).tostring(“dd.MM.yyyy”)

image

2 Likes

I think we misunderstood each other. It takes 01/07/2023 as the start date. For the end date, it will add two months to this date and get its last day, i.e. 31/08/202

@Ertan_Ay2

Start Date:

StartDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString(“dd/MM/yyyy”)
End Date
EndDate = New DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(2).Month, 1).AddDays(-1).ToString(“dd/MM/yyyy”)

1 Like

hi, thank you, it actually takes the last day, but it is necessary to add 2 months, the output is as follows(31/07/2023) gave it
desired 31/08/2023

New DateTime(DateTime.Now.Year, DateTime.Now.AddMonths(3).Month, 1).AddDays(-1).ToString(“dd/MM/yyyy”)
“31/08/2023”

@Ertan_Ay2

Well, in that case, please use these expressions:

startDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month+1, 1).ToString("dd/MM/yyyy")

endDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month + 3, 1).AddDays(-1).ToString("dd/MM/yyyy")

image

Hope this helps,
Best Regards.

Hi @Ertan_Ay2

For the Last Date:

EndDate=Datetime.ParseExact(startDate,“dd.MM.yyyy”,Globalization.CultureInfo.InvariantCulture).AddMonths(3).AddDays(-1).ToString(“dd.MM.yyyy”)

this will give you 31.08.2023
image

no it won’t, the start date is “Datetime.now.ToString(“01.MM.yyyy”)” = (01/06/2023)
If End Date = 08/31/2023

Hi @Ertan_Ay2 ,

Could you maybe try the below :

EndDateTemp = Datetime.now.AddMonths(2)
EndDate = DateTime.DaysInMonth(EndDateTemp.Year,EndDateTemp.Month).ToString+EndDateTemp.ToString(".MM.yyyy")

Here, we are using EndDateTemp variable of DateTime type to store the next month date (from the current date), we then get the actual End Date of the month with the next Expression, by calculating the Total Days in Month.

Two expressions are used, just so we are reducing the redundant expression.

Hi @Ertan_Ay2

StartDate: System.DateTime.Now.ToString(“01.MMM.yyyy”)
EndDate: Convert.ToDateTime(StartDate).AddMonths(3).AddDays(-1).ToString(“dd.MMM.yyyy”)

Thanks

Adsız1
I did it this way, but if another month has 30 days, can it perform this operation?

@Ertan_Ay2 ,

Yes. The DaysInMonth should handle that part.

1 Like

@supermanPunch Thank you :slight_smile:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.