function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Calculate first and last date of the month in a date



I need to find a last date and first date of a month of a particular date for controller


Suppose if I select 2011/06/14 from date field ,it need to give first date of 6th month and last date of 6Th month.

Please help me..


Thanks & Regards



the first date, for obvious reasons, will be the first


integer monthnumber = 6;

Date todaysdate =;

Date start = Date.newInstance(todaysdate.year(), monthnumber, 1);


For end use daysInMonth method

integer noOfDays = Date.daysInMonth(todaysdate.year(), monthnumber);

Date end = Date.newInstance( todaysdate.year, monthnumber, noOfDays);


you can also use the toStartOfMonth to get the start date, if you are using a date instance


Thanks & Ragards


Shashikant SharmaShashikant Sharma

Use this 



Date selectedDate =; //Give your date
Date firstDate = selectedDate.toStartOfMonth();
Date lastDate = firstDate.addDays(date.daysInMonth(selectedDate.year() , selectedDate.month())  - 1);
system.debug(firstDate  + ' **** ' +lastDate );

 You can verify this in system log 



Mustafa JhabuawalaMustafa Jhabuawala
Bit shorter version

Date startDate =; 
Date endDate = startDate.addMonths(1).addDays(-1);