Hi can anyone help me out how to extract string of the day from given date i am getting date in mm/dd/yyyy format from this i want to get day in the string format in apex class
Below is My code in the for loop i want to check the week day is equal to sunday or not and print the count varabile soo to compare i am getting each day in for loop in the form of mm/dd/yyyy format. i need day of the week in String like sunday, monday and soo on.Basically i want to count number of Sunday when the given input is month and year
System.debug('$$$$'+ month); System.debug('$$$'+ year); if(Date.isLeapYear(year)) { numberDays = date.daysInMonth(year, 2); temp = numberDays; for(Integer i=1;i<=temp;i++) { date myDate = date.newInstance(year, month, i); String dayString = myDate.format(); // Here i made use of format('EEEE') to get day of the week it is throwing an error String daystr = String.Valueof(myDate.day());// Here iam getting in the mm/dd/yyyy format System.debug('$$'+ daystr); if(daystr =='sunday') { count=count+1; } } } else { numberDays = date.daysInMonth(year, 2); temp = numberDays; for(Integer i=1;i<=temp;i++) { date myDate = date.newInstance(year, month, i); String dayString = myDate.format(); if(dayString =='sunday') { count++; } } } System.debug('$$$$'+ count); return count; }
Error Thrown is : Method does not exist or incorrect signature: void format(String) from the type Date.
Everthing is working fine but month ending day is not calculated, Like month has 31 days the days are calculated till 30th only same if month has 30 days. The day of 29th is calculated but day of 30th is not calculated what might be the reason
If you are getting the date in date data type then you can use the below way:
string daystr = String.Valueof(dt.day()); // dt ==> is date field
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm
Thanks,
Maharajan.C
use the below code to find the day as sunday,monday and soo on. just replace your date in sfDate variable instead of system.today().
Date startDate = date.newInstance(2001, 1, 1);
List<String> listDay = new List<String>{'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday','Saturday' , 'Sunday' };
Date sfDate = system.today(); // Replace your salesforce date field here
Integer remainder = Math.mod(startDate.daysBetween(sfDate) , 7);
String dayValue = listDay.get(remainder);
system.debug( ' ==> ' + dayValue );
Thanks,
Maharajan.C
@AuraEnabled
public static Integer calfeb(Integer month, Integer year)
{
Integer count=0;
Integer numberDays;
Integer temp;
List<String> listDay = new List<String>{'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday','Saturday' , 'Sunday' };
System.debug('$$$$'+ month);
System.debug('$$$'+ year);
if(Date.isLeapYear(year))
{
numberDays = date.daysInMonth(year, 2);
temp = numberDays;
for(Integer i=1;i<=temp;i++)
{
date myDate = date.newInstance(year, month, i);
String dayString = myDate.format(); // Here i made use of format('EEEE') to get day of the week it is throwing an error String daystr = String.Valueof(myDate.day());// Here iam getting in the mm/dd/yyyy format
System.debug('$$'+ daystr);
if(daystr =='sunday')
{
count=count+1;
}
}
}
else
{
numberDays = date.daysInMonth(year, 2);
temp = numberDays;
for(Integer i=1;i<=temp;i++)
{
date myDate = date.newInstance(year, month, i);
String dayString = myDate.format();
if(dayString =='sunday')
{
count++;
}
}
}
System.debug('$$$$'+ count);
return count;
}
Error Thrown is : Method does not exist or incorrect signature: void format(String) from the type Date.
But many have posted to use this in format();
Try the below changes :
date dt = system.today();
Datetime dtm = (Datetime) dt;
system.debug( ' ==> ' + dtm.format('EEEE') );
@AuraEnabled
public static Integer calfeb(Integer month, Integer year)
{
Integer count=0;
Integer numberDays;
Integer temp;
List<String> listDay = new List<String>{'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday','Saturday' , 'Sunday' };
System.debug('$$$$'+ month);
System.debug('$$$'+ year);
if(Date.isLeapYear(year))
{
numberDays = date.daysInMonth(year, 2);
temp = numberDays;
for(Integer i=1;i<=temp;i++)
{
date myDate = date.newInstance(year, month, i);
Datetime dtm = (Datetime) myDate;
String dayString = dtm.format('EEEE');
//String daystr = String.Valueof(myDate.day());// Here iam getting in the mm/dd/yyyy format
System.debug('$$'+ dayString);
if(dayString =='sunday')
{
count=count+1;
}
}
}
else
{
numberDays = date.daysInMonth(year, 2);
temp = numberDays;
for(Integer i=1;i<=temp;i++)
{
date myDate = date.newInstance(year, month, i);
Datetime dtm = (Datetime) myDate;
String dayString = dtm.format('EEEE');;
if(dayString =='sunday')
{
count++;
}
}
}
System.debug('$$$$'+ count);
return count;
}
Thanks,
Maharajan.C
Everthing is working fine but month ending day is not calculated, Like month has 31 days the days are calculated till 30th only same if month has 30 days. The day of 29th is calculated but day of 30th is not calculated what might be the reason