+ Start a Discussion
MicheleMcgeoyMicheleMcgeoy 

day_in_week function gives error

I'm trying to assess if the day of week is Monday, but get the following error:

Method does not exist or incorrect signature: DAY_IN_WEEK(Date)

 

If (DAY_IN_WEEK(Camp.startdate) = 2) {
// do something
} else {
// do nothing
}

 

Any ideas?

Thanks, Michele

GlynAGlynA

Michelle,

 

DAY_IN_WEEK is a date function in SOQL, but does not exist in Apex.  I have a couple of methods that might help:

//  returns the day-of-the-week for the first day of the given year
//  0 = Monday, ... 6 = Sunday
//  valid for years between 2001 and 2099 inclusive
//  Note that 2001 began on a Monday (day 0)
private static Integer firstDayOfYear( Integer theYear )
{
    if ( theYear < 2001 || theYear > 2099 ) return null;
    theYear -= 2001;
    return Math.mod( theYear + theYear/4, 7 );
}

//  returns the day-of-week for the given date
//  0 = Monday, ... 6 = Sunday
private static Integer dayOfWeek( Date theDate )
{
    Integer firstDay = firstDayOfYear( theDate.year() );
    if ( firstDay == null ) return null;
    return Math.mod( firstDay + theDate.dayOfYear() - 1, 7 );
}

 Note that dayOfWeek() returns 0 for Monday, which is different from the DAY_IN_WEEK() method in SOQL, which returns 2.

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them.  Thanks!

 

-Glyn

Sebastian MuñizSebastian Muñiz
Thanks, helped me a lot!