Sunday, January 18, 2015

Change date format in AX

Here is a useful method we can use to convert date format.

public static str dateFormatExtended2Str_DNT(Date                   _dateValue,
                                             DNT_DateFormatExtended _dateFormatExtended,
                                             int                    _separator)
{
    #define.123 (123)
    #define.132 (132)
    #define.213 (213)
    #define.231 (231)
    #define.312 (312)
    #define.321 (321)
    #define.0   (0)
    #define.2   (2)
    #define.4   (4)

    str ret;
    ;

    switch(_DateFormatExtended)
    {
        case DNT_DateFormatExtended::ddmmyy:    ret = date2str(_dateValue,#123,#2,_separator,#2,_separator,#2); break;
        case DNT_DateFormatExtended::ddmmyyyy:  ret = date2str(_dateValue,#123,#2,_separator,#2,_separator,#4); break;
        case DNT_DateFormatExtended::ddyymm:    ret = date2str(_dateValue,#132,#2,_separator,#2,_separator,#2); break;

        case DNT_DateFormatExtended::ddyyyymm:  ret = date2str(_dateValue,#132,#2,_separator,#2,_separator,#4); break;
        case DNT_DateFormatExtended::mmddyy:    ret = date2str(_dateValue,#213,#2,_separator,#2,_separator,#2); break;
        case DNT_DateFormatExtended::mmddyyyy:  ret = date2str(_dateValue,#213,#2,_separator,#2,_separator,#4); break;
        case DNT_DateFormatExtended::mmyydd:    ret = date2str(_dateValue,#231,#2,_separator,#2,_separator,#2); break;
        case DNT_DateFormatExtended::mmyyyydd:  ret = date2str(_dateValue,#231,#2,_separator,#2,_separator,#4); break;
        case DNT_DateFormatExtended::yyddmm:    ret = date2str(_dateValue,#312,#2,_separator,#2,_separator,#2); break;
        case DNT_DateFormatExtended::yyyyddmm:  ret = date2str(_dateValue,#312,#2,_separator,#2,_separator,#4); break;
        case DNT_DateFormatExtended::yymmdd:    ret = date2str(_dateValue,#321,#2,_separator,#2,_separator,#2); break;
        case DNT_DateFormatExtended::yyyymmdd:  ret = date2str(_dateValue,#321,#2,_separator,#2,_separator,#4); break;
    }

    return ret;
}

OR.....

static void DateConvert(Args _args)
{
    str     toConvert;
    str     stMonth;
    str     stDay;
    str     stYear;
    str     sDate;
    int     curPos;
    int     commaPos;
    
    date    finalDate;
    ;
    // Assign the date to be converted.
    toConvert = ‘January 4,2008’;
    
    // Retrieve the month.
    curPos = strfind(toConvert," ",1,strLen(toConvert));
    stMonth = subStr(toConvert,1,curPos-1);
    
    // Retrieve the day.
    commaPos = strFind(toConvert,',',1,strLen(toConvert));
    stDay = subStr(toConvert,curPos+1,commaPos-curPos-1);
    
    // Retrieve the year.
    stYear = subStr(toConvert,commaPos+2,4);
 
    // At this point, we have the needed information. Then, build a string out of the data.
    
    switch (stMonth)
    {
        case 'January' : sDate = '1';
            break;
        case 'February' : sDate = '2';
             break;
        case 'March' : sDate = '3';
            break;
        case 'April' : sDate = '4';
             break;
        case 'May' : sDate = '5';
            break;
        case 'June' : sDate = '6';
             break;
        case 'July' : sDate = '7';
            break;
        case 'August' : sDate = '8';
             break;
        case 'September' : sDate = '9';
            break;
        case 'October' : sDate = '10';
             break;
        case 'November' : sDate = '11';
            break;
        case 'December' : sDate = '12';
             break;
    }
    
    // Add the day of the month and the year to the string.
    sDate = sDate + '/' + stDay + '/' + styear;

    // Convert the string to date.
    finalDate = str2Date(sDate,213);
 
}

1 comment:

  1. Separator values: 0 = nothing ,1 = space, 2 = period, 3 = dash, 4 = forward slash/

    ReplyDelete