D365FO – AX – date2Str Function – Converts the specified date to a string

Not so much an article but something which is very useful. If you are coding in X++ this is a function you will use a lot. If you don’t provide the correct or valid values to the formatting parameters if the specified values are not valid.

To use the date format that the user specified in Regional Settings, use the strFmt or date2Str function with -1 in all the formatting parameters. When the regional settings control the date format, the settings can change from user to user. If -1 is used for either separator parameter, both separators default to Regional Settings.

The sequence parameter values must be any three digit number that contains exactly one occurrence of each digit 1, 2 and 3. The digits 1-2-3 represent day-month-year respectively. For example, 321 would produce the sequence of year, month, and day. Or the value can be -1 to use Regional Settings. No enumeration type should be used for this parameter, because numbers like 321 exceed the range of valid values for enumeration values, which is 0 through 250 inclusive.

The default value of the Flags parameter is the DateFlags::None enumeration value, which means no left-to-right or right-to-left sequence processing is performed.

Here is the description of the function and also a couple of examples on how to implement.


str date2Str( date date, int sequence, int day, int separator1, int month, int separator2, int year [, int flags = DateFlags::None])
ParameterDescription
dateThe date to convert.
sequenceA three digit number that indicates the sequence for the components of the date, 1 for day, 2 for month, and 3 for year.
dayDateDay enumeration value that indicates the format for the day component of the date.
separator1DateSeparator enumeration value that indicates the separator to use between the first two components of the date.
monthDateMonth enumeration value that indicates the format for the month component of the date.
separator2DateSeparator enumeration value that indicates the separator to use between the last two components of the date.
yearDateYear enumeration value that indicates the format for the year component of the date.
flagsA DateFlags enumeration value that indicates whether the language settings on the local computer should be used to calculate the proper left-to-right or right-to-left sequence in the returned string.
{
    date currentDate = today();
    str s;
    int iEnum;
    ;
    s = date2Str
        (currentDate,
        321,
        DateDay::Digits2,

        DateSeparator::Hyphen, // separator1
        DateMonth::Digits2,
        DateSeparator::Hyphen, // separator2

        DateYear::Digits4
        );
    info("Today is:  " + s);
}
/** Example Infolog output
Message (12:36:21 pm)
Today is:  2009-01-13
**/
static void Job1(Args _args)
{
    date dateformat;    
    str strformat,strformat2,strformat3,strformat4,strformat5,strformat6,strformat7,strformat8,strformat9,strformat10,strformat11;    
    dateformat = today();    
    info(strFmt("Todat is %1",dateformat));    
    strformat = date2str(dateformat,123,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Slash,DateYear::Digits4);   
 
strformat2 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Slash,DateYear::Digits4);    

strformat3 = date2str(dateformat,312,DateDay::Digits2,DateSeparator::Dot,DateMonth::Digits2,DateSeparator::Dot,DateYear::Digits4);    

strformat4 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits2);    

strformat5 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::None,DateMonth::Digits2,DateSeparator::None,DateYear::Digits2);    

strformat6 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Space,DateMonth::Digits2,DateSeparator::Space,DateYear::Digits2);    

strformat7 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Dot,DateMonth::Digits2,DateSeparator::Dot,DateYear::Digits2);    

strformat8 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::None,DateMonth::Digits2,DateSeparator::None,DateYear::Digits4);    

strformat9 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Space,DateMonth::Digits2,DateSeparator::Space,DateYear::Digits4);    

strformat10 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Dot,DateMonth::Digits2,DateSeparator::Dot,DateYear::Digits4);    

strformat11 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Slash,DateYear::Digits4);     

info(strFmt("%1",strformat));    
info(strFmt("%1",strformat2));    
info(strFmt("%1",strformat3));    
info(strFmt("%1",strformat4));    
info(strFmt("%1",strformat5));    
info(strFmt("%1",strformat6));    
info(strFmt("%1",strformat8));    
info(strFmt("%1",strformat9));    
info(strFmt("%1",strformat10));    
info(strFmt("%1",strformat11));
}

sstatic void Job1(Args _args){    date dateformat;    str strformat,strformat2,strformat3,strformat4,strformat5,strformat6,strformat7,strformat8,strformat9,strformat10,strformat11;    ;    dateformat = today();    info(strFmt(“Todat is %1”,dateformat));    strformat = date2str(dateformat,123,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Slash,DateYear::Digits4);    strformat2 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Slash,DateYear::Digits4);    strformat3 = date2str(dateformat,312,DateDay::Digits2,DateSeparator::Dot,DateMonth::Digits2,DateSeparator::Dot,DateYear::Digits4);    strformat4 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits2);    strformat5 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::None,DateMonth::Digits2,DateSeparator::None,DateYear::Digits2);    strformat6 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Space,DateMonth::Digits2,DateSeparator::Space,DateYear::Digits2);    strformat7 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Dot,DateMonth::Digits2,DateSeparator::Dot,DateYear::Digits2);    strformat8 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::None,DateMonth::Digits2,DateSeparator::None,DateYear::Digits4);    strformat9 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Space,DateMonth::Digits2,DateSeparator::Space,DateYear::Digits4);    strformat10 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Dot,DateMonth::Digits2,DateSeparator::Dot,DateYear::Digits4);    strformat11 = date2str(dateformat,213,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Slash,DateYear::Digits4);     info(strFmt(“%1”,strformat));    info(strFmt(“%1”,strformat2));    info(strFmt(“%1”,strformat3));    info(strFmt(“%1”,strformat4));    info(strFmt(“%1”,strformat5));    info(strFmt(“%1”,strformat6));    info(strFmt(“%1”,strformat8));    info(strFmt(“%1”,strformat9));    info(strFmt(“%1”,strformat10));    info(strFmt(“%1”,strformat11));}

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...