You need to sign in to do that
Don't have an account?
Hitesh 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.
I am using above code but it is throwing CPU time limit exceded inside loop
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
All Answers
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
I tried your end but it is not giving weekdays.
Please check once below code :
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
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
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