Calculate dates after 3 months

Hi everyone,
I am trying to get the specific dates after certain months. For example, if I have a 2018/1/1, after 3 months, I want to get 2018/3/31. I use this: begindate. addmonth(3). adddays(-1). This works fine until I have some interesting dates. Please see the results I tested below. Does anyone know how the dates marked wrong? And how can I solve this? Many thanks!

Begin date End date
2018/1/1 2018/3/31
2018/2/1 2018/4/30
2018/2/28 2018/5/27(Should be 2018/5/30)
2018/3/1 2018/5/31
2018/4/30 2018/7/29(Should be 2018/7/30)

@Vivian

Do you need end of month date after 3 months, then use below below code

If(
dt1.AddMonths(3).Day=1,
dt1.AddMonths(3).AddDays(-1),
dt1.AddMonths(3).AddDays(DateTime.DaysInMonth(dt1.AddMonths(3).Year, dt1.AddMonths(3).Month)-dt1.Day)
)

Hi,

Try this code,

myDate = Now
daysInMonth = Date.DaysInMonth(MyDate.Year, MyDate.Month + 3)
lastDayInMonthDate = New Date(MyDate.Year, MyDate.Month + 3, DaysInMonth)

Variable - Datatype
myDate - Datetime
daysInMonth - Int32
lastDayInMonthDate -Datetime

Hi Madhuraj,
thanks for the reply. I am trying to get the date after 3 months, not exactly to be the end of month. For instance, if I have a 2018/1/13, I want to get 2018/3/12. My code works ok until I put in 2018/2/28 and 2018/4/30, which happened to be the end of month. I don’t know if the reason are the different days in months.

Hi Sarathi123,
I am trying to get not only the last day in month, but also the specific days after 3 months. For instance, if I have a 2018/1/13, I want to get 2018/3/12. Mine works for the other days expect for the 2/28, 4/30, which happens to be the last day of each month. any idea on this? many thanks.

Add another condition for end of month dates in your code.

HI…check your format of date I think you have not given “dd” that’s why it is considering only one digit.