You need to sign in to do that
Don't have an account?
degmo
Date calculation in Apex
Hi,
I have a requirement where I need to calculate a date that is one year from today. The new date has to be same month, same week, and same day for next year.
For example, today is 5/4/2021 which is the 5th month of the year, 1 week of the month, and 4th day of the week. I am thinking I can do this by just adding 365 to the date. Is that correct or does anyone see an edge case that could make this incorrect?
I have a requirement where I need to calculate a date that is one year from today. The new date has to be same month, same week, and same day for next year.
For example, today is 5/4/2021 which is the 5th month of the year, 1 week of the month, and 4th day of the week. I am thinking I can do this by just adding 365 to the date. Is that correct or does anyone see an edge case that could make this incorrect?
You can try this:
Date currentDate =System.today();
Date dt= Date.newInstance(currentDate.year()+1, currentDate.month(), currentDate.Day());
System.debug('******'+dt);//2022-05-04 00:00:00
Please mark it best answer if its helps or do let me know if you have any queries.
Thanks!