Saturday, July 27, 2019

X++ | convert string to UTC date time

Let say we had an UTC date time which was kept in 'String'. And now we'd like to display it with the client setting time zone. We can do it as below.


 static void Job10(Args _args)  
 {  
   // 1) Save UTC date time to string  
   str          strValue = DateTimeUtil::toStr(DateTimeUtil::utcNow());  
   utcdatetime  utcValue;  
   boolean      isUTCDateTime = false;  
   ;  
   info(strFmt('Value (as saved string) = %1', strValue));  
   // 2) convert 'Saved' String to UTC date time  
   utcValue = DateTimeUtil::parse(strValue);         
   if (utcValue != utcDateTimeNull())  
     isUTCDateTime = true;  
   // 3) convert UTC date time to string with apply time zone offset  
   if (isUTCDateTime)  
   {  
     strValue = DateTimeUtil::toStr(DateTimeUtil::applyTimeZoneOffset(utcValue,  
                                                                      DateTimeUtil::getUserPreferredTimeZone()));  
   }  
   info(strFmt('Value (after convert) = %1', strValue));  
   info('done');  
 }  


The result is here.






































Until the next post!