Parsing Date Strings

Dynamics AX provides functions for parsing date strings into a date object but they are limited at best.

The following static method uses the .Net date parsing functionality to create a X++ date object from any valid date string without knowing the year, month, and day sequence or anything else about it for that matter.

It also allows you to specify the value that is returned in case of an invalid string being passed.

static date ParseDateString(str _dateValue, date _default = datenull())
{
    date    res;
    int     infoCnt;
;
    new InteropPermission(InteropKind::ClrInterop).assert();
    try
    {
        infoCnt = infolog.line();
        res = System.DateTime::Parse(_dateValue);
    }
    catch
    {
        res = _default;
        infolog.clear(infoCnt);
    }
    CodeAccessPermission::revertAssert();

    return res;
}

Enjoy.

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