Get last day of a month in a specific month

Hello,

I am currently attempting to write a function to get the last day of a month, on a given month, using .AddMonths. I have got the code for getting the given month, using a pre-defined integer variable:

StringVar = now.AddMonths(IntVar).ToString(“dd/MM/yyyy”

And here is where I got to with getting the last day of the month, but it is throwing a validation error saying value of type date cannot be converted to integer:

StringVar = New DateTime (now.Year, now.AddMonths(IntVar), DateTime.DaysInMonth(now.Year, now.AddMonths(IntVar))).ToString(“dd/MM/yyyy”)

How can I get the last day of any given month, by using .AddMonths to alter the month? Any help is appriciated. Thanks in advance

Let MonthOffset be an integer indicating how many months from the current month you want. E.g, for value -3, the month would be March 2020.

Use Now.AddMonths(MonthOffset).AddDays(DaysInMonth(Now.AddMonths(MonthOffset).Year, Now.AddMonths(MonthOffset).Month) - Now.AddMonths(MonthOffset).Day).

3 Likes

@Anthony_Humphries,

Thank you very much, this has worked, but I had to add DateTime. in front of DaysInMonth as Studio had tried declaring it as a variable.

Thanks for your help!

Yeah, namespaces can be tricky in UiPath. I’ve even seen a few cases where you need System.Datetime.DaysInMonth for UiPath to recognize it.

1 Like

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