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:
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).