How to convert UTC Date Time to String using X++?

How to convert the UTC datetime to a string in X++? 

Purpose: Whenever a user creates a record with a UTC Datetime value, the exact time may not be displayed correctly in SQL. To retrieve the precise time, we convert the UTC DateTime value to a string format. 

High Resolution steps 

  • Create the field with datatype utcdatetime 
  • Create a runnable class to convert utcdatetime to string. 

Detailed Resolution steps: 

Step 1: Create the utc datetime field in the table 

Step 2 :Create a runnable class to convert the utcdatetime to string  

Code Snippet: 

internal final class MANDateClass 

{ 

    ///  

    /// Class entry point. The system will call this method when a designated menu  

    /// is selected or when execution starts and this class is set as the startup class. 

    ///  

    /// The specified arguments. 

    public static void main(Args _args) 

    { 

        MANDateClass manDateClass = new MANDateClass(); 

        manDateClass.expirydatetime(); 

    } 

 

    public void expirydatetime() 

    { 

        MANDepartmentTable manDepartmentTable; 

        while select manDepartmentTable 

        { 

            utcdatetime    utcExpiryDate  =manDepartmentTable.PeriodCompletion; 

            str expiryDate       = DateTimeUtil::toStr(DateTimeUtil::applyTimeZoneOffset(utcExpiryDate, 

                                                       DateTimeUtil::getUserPreferredTimeZone())); 

            info(strFmt('Value (after convert) = %1',expiryDate )); 

        } 

    } 

 

} 

Code: 

Output: 

  1. In SQL,the below screenshot the utcdatetime will not show the exact time  

2. After running the runnable class, the exact utcdatetime is shown.