Hello Guys,
How to check day of week for a specific date value?
For example, I need something like: _DayOfWeek("2026-01-08") that would return 4 (Thursday).
Any suggestions?
You can use the Lookup() function for that.
In the future, not in the past. Specifically, to check what day of the week a given date will fall on.
This is a very famous problem and many have written algorithms for it.
From wiki
see Tomohiko Sakamoto's Algorithm
dayofweek(y, m, d) /* 1 <= m <= 12, y > 1752 (in the U.K.) */
{
static int t[] = {0, 3, 2, 5, 3, 0, 5, 1, 4, 6, 2, 4};
if ( m < 3 )
{
y -= 1;
}
return (y + y/4 - y/400 + y/100 + t[m-1] + d) % 7;
}
Easy to rewrite in AFL
Determination of the day of the week - Wikipedia
Calculate difference between given day and today in days. Calculate modulus 7 (reminder from division) and knowing what day is today you would now what day given date is.
Along the line of what Tomasz wrote, here is what I've used in several apps. In this way DateTimeDiff() is doing the details of leap year stuff -
// futuredtstr is input
futuredt = StrToDateTime( futuredtstr );
// This form allows negative offsets from current last date
futurewd = ( LastValue( DayOfWeek() ) + ( DateTimeDiff( futuredt, LastValue( DateTime() ) ) / (60 * 60 * 24) ) % 7 ) % 7;
My code variation
_datestring="2026-01-08";
_datetime=StrToDateTime(_datestring);
//_datetime=DateTimeAdd(_datetime, 1, inDaily);//from date plus x days
_dayNum = DateTimeConvert(0, _datetime);
_weekDay = (_dayNum +1)% 7;
"_weekDay: "+_weekDay;
You oversimplified and your solution is incorrect. Try it with 2026-02-08 and you’ll see that it produces the wrong DOW.
Yes, you are right.
//https://www.geeksforgeeks.org/dsa/find-day-of-the-week-for-a-given-date/
// Function to calculate the day of the week using the formula-based approach
function f_dayOfWeek( _datetime )
{
_datetime_DateOnly = DateTimeToStr( _datetime, 4 ); //iso date only YYYY-MM-DD
y = StrToNum( StrMid( _datetime_DateOnly, 0, 4 ) );
m = StrToNum( StrMid( _datetime_DateOnly, 5, 2 ) );
d = StrToNum( StrMid( _datetime_DateOnly, 8, 2 ) );
// Predefined month codes for each month
monthCode[0] = 6;
monthCode[1] = 2;
monthCode[2] = 2;
monthCode[3] = 5;
monthCode[4] = 0;
monthCode[5] = 3;
monthCode[6] = 5;
monthCode[7] = 1;
monthCode[8] = 4;
monthCode[9] = 6;
monthCode[10] = 2;
monthCode[11] = 4;
// Adjust year for January and February
if( m < 3 )
{
y -= 1; // If month is January or February, treat them as part of the previous year
}
// Calculate the year code
yearCode = ( y % 100 ) + ( y % 100 ) / 4;
// Adjust year code for the century
yearCode = ( yearCode + ( y / 100 ) / 4 + 5 * ( y / 100 ) ) % 7;
// Calculate the day of the week and return the value as an integer
return ( d + monthCode[m - 1] + yearCode ) % 7;
}
//_datetime = DateTimeAdd( LastValue( DateTime() ), 1, inDaily );
_datetime = StrToDateTime( "2026-02-08" );
result = floor( f_dayOfWeek( _datetime ) );
"f_dayOfWeek: " + result;
futuredtstr = "2026-12-12";
futurewd = DateTimeFormat( "%w", StrToDateTime( futuredtstr ) );
// returns string
// also subject to upper limit of StrToDateTime() depending on AB version 6/7
That is Zeller's algo, but I think Sakamoto(the one I posted) is the more modernly used approach.
The lookup table is a bit more efficient.
In my plugin journey, some optimizations came better with switch-case vs longer if-else(for runtime strings this was better), and the learning continues.
Sorry, I forgot that I already implemented that functionality in DateTimeConvert function:
function StrToWeekday( string )
{
return DateTimeConvert( 9, StrToDateTime( string ) );
}
printf("%g\n", StrToWeekday("2026-01-09") );
In Release Notes it is documented:
CHANGES FOR VERSION 6.16.0 (as compared to 6.15.0)
AFL: DateTimeConvert supports now more formats, format = 6 - day, 7 - month, 8 - year, 9 - day of week, 10 - day of year, 11 - quarter (1..4)