Saturday, December 31, 2011

DateSeparator: What's missing in the documentation of date2Str

The function date2Str provides a good way to convert a date to a string in Dynamics Ax.
The string output can be controlled, you can specify the notation with different parameters.
It goes something like this:

   str date2Str(
    date date,
    int sequence,
    int day,
    int separator1,
    int month,
    int separator2,
    int year
    [, int flags = DateFlags::None])


In an example:

static void Example_date2str(Args _args)
{   ;
    info(date2str(systemdateget(),123,2,1,2,1,4));
}


But the documentation lacks a bit of details around the different separators to use.



The different options are all listed here, but the actual values to use in the date2Str function are omitted.  The function requires an integer (or enum) as parameter for the date separator.
And not a char or string, like "/" or "-".
You can use the enum DateSeparator for this.  This enum has following enumerations:

NameValueDescription
Auto99Auto
None0None
Space1
Dot2.
Hyphen3-
Slash4/

This part of the documentation got updated with the newly released Ax 2012 version.  So the missing info only applies to versions 3.0, 4.0 and 2009.  (Like you can find both in Ax help system - see screendump above- or online at MSDN over here.)
Same example as above, but less cryptic:

static void Example_date2str(Args _args)
{   ;
    info(date2str(systemdateget(),
        123,
        dateDay::Digits2,
        DateSeparator::Space,
        DateMonth::Digits2,
        DateSeparator::Space,
        DateYear::Digits4));
}

Good to note here as well is that there is another way of setting the notation: Use the regional settings of the user.
For that, just set all the formatting parameters to -1.

Like this:

static void Example_date2str(Args _args)
{   ;
    info(date2str(systemdateget(),-1,-1,-1,-1,-1,-1));
}

Dynamics Ax actually has a predefined function for this in the Global class, being date2StrUsr.
static void Example_date2str(Args _args)
{   ;
    info(date2strUsr(systemdateget()));
}

This function depends on date2Str, just like in the example above.  From the Ax source code:
static TempStr date2StrUsr(date transDate,int flags = DateFlags::None)
{
    return date2str(transDate,-1,-1,-1,-1,-1,-1,flags);
}

4 comments:

  1. In AX 3.0 (SP3) the seperator enum is called 'DateSep' and has the following values:
    Name - Label - EnumValue
    Slash '/' 0
    Dash '-' 1
    Dot '.' 2
    Space ' ' 3
    None '#' 4

    best reagards
    Timon

    ReplyDelete
  2. this functions comes from Concorde XAL, where separator description is full.

    ReplyDelete
  3. Thanks for all the information, it was very helpful I really like that you are providing information. Great job you people are doing with this website.
    Oracle Fusion HCM Technical Online Training

    ReplyDelete
  4. I really like you post,Thanks for your sharing.

    ดูหนังผี

    ReplyDelete