Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

date function not working.

(0) ShareShare
ReportReport
Posted on by 148

System.datetime invoiceDateTime ;
str newDateTime = date2Str(retailTransactionTable.transDate,321,DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4)   ' '   time2Str(retailTransactionTable.transTime,TimeSeparator::Colon, TimeSeparator::Colon);
invoiceDateTime = str2Datetime(newDateTime, 321);

When I'm trying above code. I'm getting error.

Please advise.

  • Prasanth Profile Picture
    Prasanth 148 on at
    RE: date function not working.

    Thank you Martin.

    Try and will update.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: date function not working.

    Of course it is possible.

    One approach is what you did with date2Str() and timeStr(). If you have a problem with it, you'll need to describe the problem. Saying that you're getting an error somewhere isn't very useful.

    Another approach is using Format() function of System.String. For example:

    utcDateTime transDateTime = DateTimeUtil::newDateTime(
    	retailTransactionTable.TransDate,
    	retailTransactionTable.TransTime);
    
    str formatted = System.String::Format('{0:yyyyMMdd HH:mm:ss}', transDateTime);

  • Prasanth Profile Picture
    Prasanth 148 on at
    RE: date function not working.

    Thanks for providing the info Martin.

    is it possible to achieve my requirement ? ( I have to achieve format yyyy-MM-dd hh:mm:ss )

    for above I have date value ( retailtrasactiontable.transdate ) and time field (retailtrasactiontable.transtime )

  • Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: date function not working.

    Moved from Microsoft Dynamics AX Forum.

    Yes, I see what you're doing - and I'm explaining why it's wrong. Even if use used str2Datetime() correctly, it's useless to you, because what you want is getting a string. You have data and time and you want to convert them to a string with a specific format.

    str2Datetime() is for the opposite scenario - when you have information about date and time a string and you want to parse the string to an utcDateTime value.

  • Prasanth Profile Picture
    Prasanth 148 on at
    RE: date function not working.

    I'm using latest D365 and VS 2019.

    I'm passing date value in date2str() and getting string value ( using this function to achieve format as per requirement )

    I'm passing above str value in str2Datetime() and getting datetime value (using this function to achieve datetime values)

  • Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: date function not working.

    I'm sorry, but I still don't see the information about your version of AX. Could you please cooperate a bit and provide this information, so we don't suggest solutions that can't be used in your version of AX?

    According to your description, you want to get a string with with date and time converted to string in a specific format. Do you agree? If so, you agree that you want a string, not a utcDateTime value.

    utcDateTime doesn't have any string format. A string format applies when you want to convert utcDateTime to a string.

  • Prasanth Profile Picture
    Prasanth 148 on at
    RE: date function not working.

    My requirement is to getting date values as below format

    Format : yyyy-MM-DD hh:mm:ss

    so I'm passing date and required data in 'd' variable and again converting str to datetime ( in invoiceDateTime variable )

    if above process is not correct way, how to achieve that format.

  • Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: date function not working.

    If your goal is to get a string, then your assignment of invoiceDateTime doesn't make sense, because this variable is for datetime values, not for strings. The result you want is in 'd' variable; there is no point in converting it back to utcDateTime, even if you implemented it correctly.

    By the way, I'm still waiting for the information that you should have provided at the very beginning: a description of the error (error message, whether it's a compilation error or a runtime one, where it occurs) and your version of AX.

  • Prasanth Profile Picture
    Prasanth 148 on at
    RE: date function not working.

    Hi Martin,

    Thank you for responding.

    I created one class, which is generating CSV file. in that one date field is there but I'm getting blank values and I'm using below function [date2str() for getting date as per requirement ( format 2020-09-14 17:14:11).

    please advise what is wrong in below code.

    str d;
        utcDateTime invoiceDateTime;
        d = date2Str(10\15\2023,321,DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4);//   ' '
          time2Str(65850,TimeSeparator::Colon, TimeSeparator::Colon);
    
        invoiceDateTime     =   str2Datetime(d, 321);
    
        info(strFmt("%1",invoiceDateTime));

  • Martin Dráb Profile Picture
    Martin Dráb 230,962 Most Valuable Professional on at
    RE: date function not working.

    I guess you want something like this:

    utcDateTime invoiceDateTime = DateTimeUtil::newDateTime(
    	retailTransactionTable.TransDate,
    	retailTransactionTable.TransTime,
    	DateTimeUtil::getUserPreferredTimeZone());

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,160 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,962 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans