Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (32.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);

Browse Categories

...