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

Notifications

Announcements

Community site session details

Community site session details

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

I have the same question (0)
  • Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

    Unfortunately, there is no single solution for all possible types of error. Therefore just saying that you're getting "an error" isn't sufficient - please tell us what error you're getting.

    Also, tell us what you're trying to achieve with your code. There may be an easier way then your approach.

    Which version of AX are you using?

  • Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

    I guess you want something like this:

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

  • Prasanth Profile Picture
    148 on at

    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
    238,601 Most Valuable Professional on at

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

    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
    238,601 Most Valuable Professional on at

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

    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
    238,601 Most Valuable Professional on at

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

    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 )

  • Suggested answer
    Martin Dráb Profile Picture
    238,601 Most Valuable Professional on at

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

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…

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 650 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 287 Super User 2026 Season 1

#3
Martin Dráb Profile Picture

Martin Dráb 225 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans