Friday, October 9, 2020

X++ | convert string (as Datetime) to UTCdatetime

 If we got a data from the external application like "2020-10-07T11:55:27". We can use the below code to convert it to UTCdatetime.

The above format is occurred when user send a datetime field from .NET through OData (Edm.Datetime).

 class ConvertStr2UTCdatetime_Job_Test  
 {  
   public static void main(Args _args)  
   {  
     str     	 testStr = "2020-10-07T11:55:27";
     utcdatetime testUTCdateTime;  
     ;  
 
     testStr = strReplace(testStr, "T", " ");  
     testUTCdateTime = str2datetime(testStr, 321);  
     info(strFmt("testUTCdateTime %1", testUTCdateTime));  
     info('done');  
   }  
 }  


Until the next post!


3 comments:

  1. Hi Rum, DateTimeUtil::parse(str _testStr) should also work (in all versions which have this function).

    ReplyDelete
    Replies
    1. Hi, thank you very much for your suggestion! That is very helpful!

      Delete