web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    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

  • Suggested answer
    Kumar Gaurav @ MS Profile Picture
    on at

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

    date myDate = 17\10\2014;

  • Verified answer
    Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at

    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);
  • Lowaiy Baghdadi Profile Picture
    655 on at

    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));

  • Lowaiy Baghdadi Profile Picture
    655 on at

    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("");

  • Verified answer
    Martin Dráb Profile Picture
    237,803 Most Valuable Professional on at

    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
    655 on at

    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!

  • Suggested answer
    khadar shaik Profile Picture
    55 on at

    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.

          

  • Vilmos Kintera Profile Picture
    46,149 on at

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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans