2013年4月10日星期三

How to using .net 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;
}

没有评论:

发表评论