0 votes
1 view
in Salesforce by (11.9k points)

How do we get the first day of the month and the last day of the month? I am using it on an apex class.

I can get today's date by using

date.today()

but how would I get the first and last day of the month?

1 Answer

0 votes
by (30.1k points)

With respect to your question, you get the first date of the month (I'll use today's month in my example) using the following

Date firstDayOfMonth = System.today().toStartOfMonth();

To get the last day of the month, use the following:

Date lastDayOfMonth = firstDayOfMonth.addDays(Date.daysInMonth(firstDayOfMonth.year(), firstDayOfMonth.month()) - 1);

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...