/ Microsoft / Convert date and time to utcDateTime using X++

Convert date and time to utcDateTime using X++

Learn, share and spread knowledge

Working with date and time is a common task while working with data and tables in Microsoft Dynamics 365 for Finance and Operations and in Microsoft Dynamics AX 2012. If you want to convert date and time to utcDateTime using X++, and both date and time are stored as string in two different variables. It can be achieved using the following code:

//Declaring variable of type utcDateTime, date and time
utcdatetime utcDT;
str strDate;
str strTime;

//Assigning dummy values
strDate = "18/3/2019";
strTime = "03:06:00 pm";

//Sequence -> day - 1, month - 2, year - 3
//Sequence is 123 passed to the function str2Date. It means that 18 is day, 3 is month and 2019 is year
utcDT = DateTimeUtil::newDateTime(str2Date(strDate,123),str2Time(strTime));

//Printing utcDateTime to infolog
info(strFmt("UTC Date Time: %1", utcDT));

Output of the above code is as follows: UTC Date Time: 3/18/2019 03:06:00 pm

If you want to convert date and time to utcDateTime using X++, and both date and time are stored as single string. You should do it more easily. See the following code:

//Declaring variable of type utcDateTime and DateTime of type string
str strDateTime;
utcdatetime utcDT;

//Assigning dummy value
strDateTime = "18/3/2019 03:06:00 pm";
    
//Sequence -> day - 1, month - 2, year - 3
//Sequence is 123 passed to the function str2Date. It means that 18 is day, 3 is month and 2019 is year
//str2DateTime function converts the DateTime string to utcDateTime 
utcDT = str2DateTime(strDateTime,123);


//Printing utcDateTime to infolog
info(strFmt("UTC Date Time: %1", utcDT));

Output of the above code is as follows: UTC Date Time: 3/18/2019 03:06:00 pm

Remember that the sequence of date can be created using the following table:

Part of DateSequence Value
Day1
Month2
Year3

Some date sequences are created using the above table can be seen as following:

Date FormatSequence
year/day/month312
day/year/month132
month/day/year213

You must remember that utcDateTime stores the date with the sequence of 213. It means that the date is stored as: month/day/year.

The sequence which we have passed to the following functions in this tutorial is 123:

  • str2Time(str text)
  • str2Date(str text, int sequence)
  • str2DateTime(str text, int sequence)

Sequence 123 means that the date which I am passing consists of the format: day/month/year. It helps the function to know about the format of the string which contains the date. If your string consists of the format: month/day/year. Then, you should pass the sequence as 213.

These two code snippets which I have shared above to convert date and time to utcDateTime using X++ can be executed using job. It will print the result on the infolog.

If you found any ambiguity or better solution, please comment. If this articles helps you, please like, comment and share with community members.

Blog: Click here

YouTube: Click here

GitHub: Click here

Nothing is particularly hard if you divide it into small jobs. – Henry Ford

1 POST COMMENT

Send Us A Message Here

Your email address will not be published. Required fields are marked *

1 Comments