Last Weekday of Month

This method will calculate the date of the last specified weekday in the month of the given date.

static date lastWeekdayOfMonth(date _date, WeekDays _weekday){
    int     _endWeekDay, _diff;
    date    _endDate = endmth(_date);
;
    _endWeekDay = dayofwk(_endDate);
    _diff       = _endWeekDay - _weekday;
    _date       = ((_endDate) - _diff);

    if (_diff < 0)
        _date = ((_date) - 7);

    return _date;
}

Example:

Date     d = mkDate(1,7,2011); //July 1, 2011
WeekDays w = WeekDays::Wednesday;
;
d = lastWeekdayOfMonth(d,w);
info(date2str(d,213,1,4,1,4,4));

Will output 7/27/2011 to the infolog

Share/Bookmark
This entry was posted in AX 2009, AX 2012, AX 4.0, Microsoft Dynamics AX and tagged , , . Bookmark the permalink.