0

String format options for utcDateTime

 2 years ago
source link: https://dev.goshoom.net/2019/02/string-format-options-for-utcdatetime/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

String format options for utcDateTime

I’ve run into a problem that reminded me that while X++ types and corresponding CLR types (such as str and System.String) can often be user interchangeably, they aren’t the same.

I was trying to convert a utcdatetime value to the standard “sortable” format in D365FO, for which I wrote the following code:

utcdatetime currentDateTime = DateTimeUtil::utcNow();
str s = System.String::Format('{0:s}', currentDateTime);

What I expected was something like 2019-02-03T17:29:00, but I got a very different format – 02/03/2019 17:29:00. What was wrong with my code?

The format definition is all right… but I don’t have the right type! I can easily fix the problem by declaring currentDateTime as System.DateTime instead of utcdatetime.

System.DateTime currentDateTime = DateTimeUtil::utcNow();
str s = System.String::Format('{0:s}', currentDateTime);

When Format() method is called, it’s able to utilize special formatting options (such as the “s” format), if the type of the value has methods that knows what do to it with it. System.DateTime knows what to do, but utcdatetime is actually Microsoft.Dynamics.Ax.Xpp.AxShared.utcdatetime structure which isn’t able to handle these options. It has a single hard-coded format for converting the datetime value to a string.

While I’m still working with the same value in both utcdatetime and System.DateTime, they’re two different types with different behavior and a conversion may be needed if I need behavior specific to only one of them.

I tested this behavior on Platform Update 22 and 24.

Filed under Dynamics Community Syndication, EN | Tagged .NET Interop, D365FO, datetime, Dynamics 365, type conversion | Comment | Permalink

Leave a Reply Cancel reply

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

Comment *

Name

Email

Website

Save my name, email, and website in this browser for the next time I comment.

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK