Code highlighting

Thursday, February 23, 2012

Tutorial: Determine if a string is a valid UtcDateTime

In my post about UtcDateTime in Dynamics AX, I recently received a question about determining if a specific string is a valid UtcDateTime in X++.I've spent some time looking into it and could not find an available method that could be used for that.
Also, it was not really clear, which format should be called valid.

Anyhow, I have so far discovered 2 ways to do it in AX: the simple way, and MY way :) They produce slightly different results however. Let me know if you find other ways.

The simple way is to use the intrinsic function str2datetime.
As you can see on MSDN, it supports a couple of input formats, and if you provide a non-date value, will just ignore it silently and return an empty value.

However, when working with DateTimeUtil methods, the expected format is yyyy-mm-ddThh:mm:ss
And, as you can see from the below test job, specifically this format is not supported by str2datetime.
So I wrote my own method based on DateTimeUtil::parse(), that returns true/false based on the string value matching the above format. This is however the only supported format for DateTimeUtil, so the other 3 examples that work with str2datetime do not work here.

Anyhow, you can download the code (I've put the method into Global) and the test job from my SkyDrive.

Test job:
public static void isValidUTCDateTimeTest(utcDateTime _utcDateTime)
{
    boolean utc1 = str2datetime("2012/02/25 23:04:59", 321) != utcDateTimeNull();
    boolean utc2 = str2datetime("Feb-2012-25 11:04:59 pm", 231) != utcDateTimeNull();
    boolean utc3 = str2datetime("25 02 2012 11:04:59 pm", 123) != utcDateTimeNull();
    boolean utc4 = str2datetime("2012-02-25T23:04:59", 321) != utcDateTimeNull();
    boolean utc5 = str2datetime("XXXX", 123) != utcDateTimeNull();

    void showResult(str format, boolean isValid)
    {
        info(strFmt("%1 - %2", format, isValid));
    }

    setPrefix("Date time validation");
    setPrefix("str2datetime has multiple valid formats");
    showResult("2012/02/25 23:04:59",       utc1);
    showResult("Feb-2012-25 11:04:59 pm",   utc2);
    showResult("25 02 2012 11:04:59 pm",    utc3);
    showResult("2012-02-25T23:04:59",       utc4);
    showResult("XXXX",                      utc5);

    setPrefix("Correct UtcDateTime format for DateTimeUtil: yyyy-mm-ddThh:mm:ss");
    showResult("2012/02/25 23:04:59",      isValidUTCDateTime("2012/02/25 23:04:59"));
    showResult("Feb-2012-25 11:04:59 pm",  isValidUTCDateTime("Feb-2012-25 11:04:59 pm"));
    showResult("25 02 2012 11:04:59 pm",   isValidUTCDateTime("25 02 2012 11:04:59 pm"));
    showResult("2012-02-25T23:04:59",      isValidUTCDateTime("2012-02-25T23:04:59"));
    showResult("XXXX",                     isValidUTCDateTime("XXXX"));
}

3 comments:

  1. It took me a lot of time to find how to validate Dates, and finally you came with the solution.

    Thank you for this interesting post!

    ReplyDelete
  2. hi ...vanya
    i've got some trouble here.
    when i click button E-mail applicant at Application Detail form->Recruitment->Periodic->Human Resource i've got an error. the error said "DictField object not initialized" at class HRMApplicationbookmark/bookmarkandvalue. so i try to open that class and compile it. but there's no an error right there.

    and it's happen to my Sysdictfield too. When i open my Application E-mail template form->Recruitment->setup->Human Resource, then i click radio button for Interview, there's an error "Sysdictfield object not initialized" at class HRMApplicationemailtemplates/fillListViews ...

    may be u know the answer. And thanks for the help. :)

    ReplyDelete
  3. Well, compiling the code again won't help, because there are no application compilation errors there.
    This is a runtime error that you get when the code tries to access a certain table field.

    The easiest way to investigate this would be to set a breakpoint in that method and see what you're missing

    ReplyDelete

Please don't forget to leave your contact details if you expect a reply from me. Thank you