AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
DAX
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 24.02.2012, 01:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
Kashperuk Ivan: Tutorial: Determine if a string is a valid UtcDateTime
Источник: http://kashperuk.blogspot.com/2012/0...-is-valid.html
==============

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:
.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: Consolas, "Courier New", Courier, Monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #a31515; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
1: public static void isValidUTCDateTimeTest(utcDateTime _utcDateTime) 2: { 3: boolean utc1 = str2datetime("2012/02/25 23:04:59", 321) != utcDateTimeNull(); 4: boolean utc2 = str2datetime("Feb-2012-25 11:04:59 pm", 231) != utcDateTimeNull(); 5: boolean utc3 = str2datetime("25 02 2012 11:04:59 pm", 123) != utcDateTimeNull(); 6: boolean utc4 = str2datetime("2012-02-25T23:04:59", 321) != utcDateTimeNull(); 7: boolean utc5 = str2datetime("XXXX", 123) != utcDateTimeNull(); 8: 9: void showResult(str format, boolean isValid) 10: { 11: info(strFmt("%1 - %2", format, isValid)); 12: } 13: 14: setPrefix("Date time validation"); 15: setPrefix("str2datetime has multiple valid formats"); 16: showResult("2012/02/25 23:04:59", utc1); 17: showResult("Feb-2012-25 11:04:59 pm", utc2); 18: showResult("25 02 2012 11:04:59 pm", utc3); 19: showResult("2012-02-25T23:04:59", utc4); 20: showResult("XXXX", utc5); 21: 22: setPrefix("Correct UtcDateTime format for DateTimeUtil: yyyy-mm-ddThh:mm:ss"); 23: showResult("2012/02/25 23:04:59", isValidUTCDateTime("2012/02/25 23:04:59")); 24: showResult("Feb-2012-25 11:04:59 pm", isValidUTCDateTime("Feb-2012-25 11:04:59 pm")); 25: showResult("25 02 2012 11:04:59 pm", isValidUTCDateTime("25 02 2012 11:04:59 pm")); 26: showResult("2012-02-25T23:04:59", isValidUTCDateTime("2012-02-25T23:04:59")); 27: showResult("XXXX", isValidUTCDateTime("XXXX")); 28: }






Источник: http://kashperuk.blogspot.com/2012/0...-is-valid.html
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Kashperuk Ivan: Tutorial: refresh, reread, research, executeQuery - which one to use? Blog bot DAX Blogs 20 04.04.2017 13:23
Kashperuk Ivan: Tutorial: AX 2012 - Invalid field access or Accessing unretrieved fields Blog bot DAX Blogs 0 29.10.2011 05:18
Kashperuk Ivan: Casing and text search/comparison tutorial Blog bot DAX Blogs 6 25.02.2010 15:41
Kashperuk Ivan: UtcDateTime in Dynamics AX 2009 Blog bot DAX Blogs 0 17.02.2010 15:05
Kashperuk Ivan: tutorial_Form_Dynalink (a small tutorial on dynalinks) Blog bot DAX Blogs 0 22.08.2007 17:00

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 13:05.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.