Saturday, July 16, 2022

convert date time to string using x++ In D365FO

 Convert date time to string using x++ In D365FO.

static void dateToStr(Args _args)
{
    UtcDateTime     dateTime = DateTimeUtil::getSystemDateTime();

    DateTimeUtil::toStr(dateTime);// Convert to str : "2022-06-15T13:00:30.0900000"
---or---
    // If you want special characters.

    system.DateTime sysDateTime;

    sysDateTime =  dateTime;

    print sysDateTime.ToString("yyyy-MM-dd");
    print sysDateTime.ToString("MM/dd/yy H:mm:ss zzz");
----or----
use date2Str:

    str datestr;

    datestr = date2Str(today(),
                        321,
                        DateDay::Digits2,
                        DateSeparator::Hyphen, // separator1
                        DateMonth::Digits2,
                        DateSeparator::Hyphen, // separator2
                        DateYear::Digits4);
    
    info('Today is: ' + datestr);
}
---Or---
    utcDateTime     utcDateTimeFromString   = DateTimeUtil::anyToDateTime("2012-04-01T00:00:00");
    utcDateTime     utcDateTimeISOFormat    = 2012-04-01T00:00:00;
    utcDateTime     utcNow;
    System.DateTime systemDateTime;
    date            dateOnly;
    str             systemDateTimeStr;

    // AX UTC to System.DateTime: Result: "7/17/2022 3:57:25 AM"
    systemDateTime      = Global::utcDateTime2SystemDateTime(DateTimeUtil::utcNow());

    //System.DateTime to AX UTC: Result:"7/17/2022 03:57:52 am"
    utcNow              = Global::clrSystemDateTime2UtcDateTime(System.DateTime::get_UtcNow());

    // date portion from a UTC:  Result:"7/17/2022"
    dateOnly            = DateTimeUtil::date(utcNow);

    // Cast to string for output
    systemDateTimeStr   = systemDateTime.ToString();
     
    info(strFmt("%1, %2, %3",
                systemDateTimeStr,
                utcNow,
                dateOnly));
    
    info(strFmt("%1, %2",
                DateTimeUtil::toStr(DateTimeUtil::utcNow()),//2022-07-17T04:00:58
                DateTimeUtil::toStr(utcNow)));// 2022-07-17T04:00:59





Keep Daxing!!




No comments:

Post a Comment