How to convert a string that does not have the "mm/dd/yyyy" format or the "dd/mm/yyyy" format to a native date in Microsoft Dynamics AX 4.0

Article ID: 947373 - View products that this article applies to.
Source: Microsoft Support

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION THAT IS CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED TO SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.
Expand all | Collapse all

On This Page

INTRODUCTION

This article describes how to convert a string that does not have one of the following formats to a native date in Microsoft Dynamics AX 4.0:
  • The "mm/dd/yyyy" format
  • The "dd/mm/yyyy" format

More information

Microsoft Dynamics AX 4.0 provides conversion functions to convert a string to a date. However, the string must have one of the following formats:
  • The "mm/dd/yyyy" format
  • The "dd/mm/yyyy" format
Microsoft Dynamics AX 4.0 cannot directly convert a string to a native date if the string does not have one of both formats. For example, Microsoft Dynamics AX 4.0 cannot convert the string of "Janauary 4, 2008" to a native date.

However, you can use the Microsoft Dynamics AX 4.0 string functions to convert the string to a native date. To do this, retrieve the month, the day of the month and the year from the string, concatenate them to a string in the format that Microsoft Dynamics AX 4.0 can handle, and then convert the concatenated string to a native date. The following code is an example about how to use the Microsoft Dynamics AX 4.0 string functions to convert a string to a native date.
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);
 
}
Note The code assumes that the following conditions are true:
  • In the original string, the month is followed by a space.
  • In the original string, the day of the month is followed by a comma.
  • In the original string, the year is preceded by a space.
If one of the conditions is not true, you must modify the code.

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, THE RELIABILITY OR THE ACCURACY OF THE INFORMATION THAT IS CONTAINED IN THE DOCUMENTS AND THE RELATED GRAPHICS PUBLISHED ON THIS WEB SITE (THE “MATERIALS”) FOR ANY PURPOSE.

THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON-INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.

Properties

Article ID: 947373 - Last Review: July 14, 2012 - Revision: 3.0
Applies to
  • Microsoft Dynamics AX 4.0
  • Windows MorphX Development Suite
Keywords: 
kbexpertiseadvanced kbinfo kbnomt kbexpertisebeginner kbmbsmigrate kbhowto kbexpertiseinter KB947373

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com