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
Hitesh chaudhariHitesh chaudhari 

How to get all dates and days of current month

Hi guys,

I want to get all the dates of current month with its current day.
 
DateTime startDate = System.Date.today().toStartOfMonth(); 
        DateTime endDate = startDate.addMonths(1).addDays(-1);
        
        system.debug('Start Date '+ startDate);
        system.debug('End Date '+ endDate);
        
        for(DateTime d = startDate; d < = endDate ; d.addDays(1))
        {
            system.debug('Current date in loop is  '+ d.day());
            String dayofWeek = d.format('EEEE');
            system.debug('Weekeday is '+ dayofWeek);
        }

I am using above code but it is throwing CPU time limit exceded inside loop 
Best Answer chosen by Hitesh chaudhari
v varaprasadv varaprasad
DateTime startDate = System.Date.today().toStartOfMonth(); 
DateTime endDate = startDate.addMonths(1).addDays(-1);

system.debug('Start Date '+ startDate);
system.debug('End Date '+ endDate);





for(DateTime d = startDate; d < = endDate ; d=d.addDays(1))
{
    system.debug('Datetime    '+Date.valueOf(d));
   
    string myDate = d.date().format();            
    system.debug('Mydate   '+ myDate);
    String dayofWeek = d.format('EEEE');
    system.debug('Weekeday is '+ dayofWeek);
    
}

 

All Answers

Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Change your for-loop increment to this:
DateTime startDate = System.Date.today().toStartOfMonth(); 
        DateTime endDate = startDate.addMonths(1).addDays(-1);
        
        system.debug('Start Date '+ startDate);
        system.debug('End Date '+ endDate);
        
        for(DateTime d = startDate; d < = endDate ; d = d.addDays(1))
        {
            system.debug('Current date in loop is  '+ d.day());
            String dayofWeek = d.format('EEEE');
            system.debug('Weekeday is '+ dayofWeek);
        }

 
Maharajan CMaharajan C
Small Change Nick:

List<datetime > AllDaysList = new List<datetime >();
List<string> AllDayString = new List<String>();
Integer yearSelection = Date.Today().Year();
Integer monthSelection = Date.Today().Month();
Integer daysInMonth = Date.daysInMonth(yearSelection, monthSelection); //number of days in month

for (Integer day = 1; day <= daysInMonth; day++) { //for first to last day in month
datetime  tempdate = Date.newInstance(yearSelection, monthSelection, day);
system.debug('@@@ Date is '+ tempdate);     
String dayString = tempdate.format('EEEE');
system.debug('@@@ Weekeday is '+ dayString);     
AllDayString.add(dayString);       
AllDaysList.add(tempdate); //add the date to our list
}

system.debug('@@@ AllDaysList ' + AllDaysList);
system.debug('@@@ AllDayString '+ AllDayString);

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
Hitesh chaudhariHitesh chaudhari
Thanks for quick reply.

I tried your end but it is not giving weekdays.
v varaprasadv varaprasad
Hi Imnick,

Please check once below code : 
 
DateTime startDate = System.Date.today().toStartOfMonth(); 
        DateTime endDate = startDate.addMonths(1).addDays(-1);
        
        system.debug('Start Date '+ startDate);
        system.debug('End Date '+ endDate);
       
      
       while(startDate < endDate){
                 system.debug('Current date in loop is  '+ startDate);
                 
                  String dayofWeek = startDate.format('EEEE');
                   system.debug('Weekeday is '+ dayofWeek);
                 startDate = startDate.addDays(1);
            }

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com

 
v varaprasadv varaprasad
DateTime startDate = System.Date.today().toStartOfMonth(); 
        DateTime endDate = startDate.addMonths(1).addDays(-1);
        
        system.debug('Start Date '+ startDate);
        system.debug('End Date '+ endDate);
       
      
       


        for(DateTime d = startDate; d < = endDate ; d=d.addDays(1))
        {
            
            system.debug('Current date in loop is  '+ d);
            String dayofWeek = d.format('EEEE');
            system.debug('Weekeday is '+ dayofWeek);
            startDate = startDate.addDays(1);
        }

 
Maharajan CMaharajan C
Hi Nick,

Do you want only the weekdays that is do you want to exclude the weekends (Saturday , Sunday) if it is yes then use below code:

List<datetime > AllDaysList = new List<datetime >();
List<string> AllDayString = new List<String>();
Integer yearSelection = Date.Today().Year();
Integer monthSelection = Date.Today().Month();
Integer daysInMonth = Date.daysInMonth(yearSelection, monthSelection); //number of days in month

for (Integer day = 1; day <= daysInMonth; day++) { //for first to last day in month
datetime  tempdate = Date.newInstance(yearSelection, monthSelection, day);
String dayString = tempdate.format('EEEE');
if(dayString != 'Sunday' && dayString != 'Saturday')
{
system.debug('@@@ Weekeday is '+ dayString);
system.debug('@@@ Date is '+ tempdate); 
AllDayString.add(dayString);       
AllDaysList.add(tempdate);    
}
     
}

system.debug('@@@ AllDaysList ' + AllDaysList);
system.debug('@@@ AllDayString '+ AllDayString); 


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
Hitesh chaudhariHitesh chaudhari
Thanks for both of you  @Maharajan C and @v varaprasad.


Both of your logic is awsm and it works for me .


But small problem  i have getting, I just want Date and both of your logic gives date time .

I tried it by using date() on but it still returns datetime 

System.debug('@@@@ date val is '+startDate.date()); 

it gives output as 

13:22:13:017 USER_DEBUG [63]|DEBUG|@@@@ date val is 2018-08-01 00:00:00

i just want date not time
Expected output : date val is 2018-08-01
v varaprasadv varaprasad
DateTime startDate = System.Date.today().toStartOfMonth(); 
DateTime endDate = startDate.addMonths(1).addDays(-1);

system.debug('Start Date '+ startDate);
system.debug('End Date '+ endDate);





for(DateTime d = startDate; d < = endDate ; d=d.addDays(1))
{
    system.debug('Datetime    '+Date.valueOf(d));
   
    string myDate = d.date().format();            
    system.debug('Mydate   '+ myDate);
    String dayofWeek = d.format('EEEE');
    system.debug('Weekeday is '+ dayofWeek);
    
}

 
This was selected as the best answer
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
System.debug( '@@@@ date val is ' + startDate.format( 'yyyy-MM-dd' ) );