Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)
Answered

Converting string into datetime

(0) ShareShare
ReportReport
Posted on by 655

It seems simple thing but it's not happening... Basically I wanna import a text file (CSV) and the date field it comes with this format (mm/dd/yyyy h:mm) i.e. (4/7/2014 0:00). So, I've tried to use the datetime function but it returns with null! The code is something like this:

   str strMyDate;

   utcDateTime MyDate;

   ;

   strMyDate = '10/17/2014 0:00';

   MyDate = str2datetime(strMyDate,213);

   info("MyDate"+datetime2str(MyDate));

Note that MyDate is coming null! Any idea on this? Thanks!

*This post is locked for comments

  • Vilmos Kintera Profile Picture
    Vilmos Kintera 46,149 on at
    RE: Converting string into datetime

    Please open a new thread for your question and avoid hijacking other threads, since it is only loosely related to the original topic.

  • Suggested answer
    khadar shaik Profile Picture
    khadar shaik 55 on at
    RE: Converting string into datetime

    Hi Martin,

    I want to assign System.DateTime  value as NULL in some condition.

    How it Can be?

    Ex:  System.DateTime  dateTime;

    dateTime = dateNull();

    but here dateNull() is not compitable with this parameter.

          

  • Lowaiy Baghdadi Profile Picture
    Lowaiy Baghdadi 655 on at
    RE: Converting string into datetime

    Thanks a lot Martin, I followed your instructions and it works like a charm! Try, Catch is able now to catch the exceptions and error them out!

    Thanks again!

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,569 Most Valuable Professional on at
    RE: Converting string into datetime

    It 's because the value (10/17/2014 0:00) isn't compatible with the format ("MM/dd/yyyy") and a format exception is thrown. Follow my example exactly if you want it to work.

    Unfortunately the default exception handler doesn't show .NET exceptions in infolog, you have to explicitly catch them by yourself. For example:

    try
    {
        …
    }
    catch (Exception::CLRError)
    {
        throw error(AifUtil::getClrErrorMessage());
    }

    You could also use TryParseExact(), which returns true or false instead of throwing the exception. Nevertheless you should still wrap all .NET Interop code in try/catch because you may get other exceptions as well.

  • Lowaiy Baghdadi Profile Picture
    Lowaiy Baghdadi 655 on at
    RE: Converting string into datetime

    Martrin,

    I tried your .Net Interop but the compiler stops and exists after the last line! I tried to add info(datetime2str(dt)); in the next the line but it never continues to the info line!

    System.Globalization.CultureInfo culture ;

    utcDateTime dt ;

    ;

    culture = System.Globalization.CultureInfo::get_InvariantCulture();

    dt = System.DateTime::ParseExact('10/17/2014 0:00','MM/dd/yyyy',culture);

    info(datetime2str(dt));

    info("");

  • Lowaiy Baghdadi Profile Picture
    Lowaiy Baghdadi 655 on at
    RE: Converting string into datetime

    Well, since the time part is not coming in a regular format, I tried to cut it from the text using strReplace function. I know it's not recommended, but it does the job! 

    str strMyDate;
    date MyDate;

    strMyDate = '4/7/2014 0:00';
    strMyDate = strReplace(strMyDate,' 0:00','');
    MyDate = str2Date(strMyDate,213);

    info("MyDate"+date2str(MyDate),213));

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,569 Most Valuable Professional on at
    RE: Converting string into datetime

    If you have to deal with custom formats, use DateTime.ParseExact() through .NET Interop:

    System.Globalization.CultureInfo culture
        = System.Globalization.CultureInfo::get_InvariantCulture();
    
    utcDateTime dt = System.DateTime::ParseExact(
                                '10/17/2014 0:00',
                                'MM/dd/yyyy H:mm',
                                culture);
  • Suggested answer
    RE: Converting string into datetime

    alternatively if you want to initialize date you can directly do this,

    date myDate = 17\10\2014;

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Converting string into datetime

    I think issue is with date string format

    it should be

    myDate = '10/17/2014 0:00:00';

    in your case it is

    strMyDate = '10/17/2014 0:00';

    so the time format is not good enough

    it should be HH:MM:SS

    please try following job

    static void Job19(Args _args)

    {

       str myDate;

       utcDateTime MyDateTime;

       myDate = '10/17/2014 0:00:00';

       myDateTime = str2datetime(myDate, 213);

       info(strfmt("Mydate %1", myDateTime));

    }

    Please verify and let me know if this information helps you

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,883 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,569 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans