Dynamics AX – All about dates and X++

By Sourabh Khosla | June 17, 2014

Well, we have all been there… you are working with dates in Dynamics AX and you realize there are tons of functions out there for dates… which one to pick?  And actually almost all the time I cannot find what I need.

So, I am just going to write out everything I can recall about date functions at this point and we will go from there.

  • I will start with the Current Business Date.
    Generally, I have used systemDateGet() which holds the logic business date of the system. There is one more function called today() which gives the actual machine date. So far I haven’t felt the need to use today() but if anyone needs, it is out there.
    systemDateGet()
    today()There is one global method called getSystemDateTime(); which sets the current UTC time as a utcdatetime type. Only DateTimeUtil::getSystemDateTimecompensates for the time zone of the user, so as a best practice we should use this one instead of systemDateGet()

 

  • There are functions out there which will allow you to get Day of Month, Month of Year, and so on.
    dayOfMth(systemdateget());
    dayOfYr(systemdateget());
    wkOfYr(systemdateget());
    mthOfYr(systemdateget());
    dayName(dayOfMth(systemdateget()));

 

  • When we need to get the next month, next year or may be next quarter, We have:
    nextMth(systemdateget());
    prevMth(systemdateget());
    nextYr(systemdateget());
    preYr(systemdateget());
    prevQtr(systemdateget());
    nextQtr(systemdateget());
    dateMthFwd(systemdateget(), 9);  // forward 9 months
    dateMthFwd(systemdateget(), -9);  // backward 9 Months

 

  • Creating your own date
    mkdate : Creates a date based on three integers, which indicate the day, month, and year, respectively.
    e.g. mkDate(09, 09, 1989);

 

  • Date to String conversion
    date2str(systemdateget(), 123, dateDay::Digits2, DateSeparator::Space, DateMonth::Digits2, DateSeparator::Space, DateYear::Digits4);There is one more method available in case we want to use the regional settings of the user.
    date2strUsr(systemdateget());
  • UTCDateTime conversion
    Just so that you know, values of type UTCDateTime are stored in the database in UTC time zone while in the User Interface UTCDateTime values are displayed in  a user preferred time zone.  And the regular Date and Time values are not related to any time zone, so both stored and displayed values are equal.

 

public static void utcDateTimeConversion()
{
utcDateTime dateTimeUTC;
date regularDate;
TimeOfDay regularTime;

dateTimeUTC = DateTimeUtil::utcNow();

// converting UTC date/time to regular user time zone date
regularDate= DateTimeUtil::date(DateTimeUtil::applyTimeZoneOffset(dateTimeUTC ,
DateTimeUtil::getUserPreferredTimeZone()));

// converting UTC date/time to regular user time zone time
regularTime= DateTimeUtil::time(DateTimeUtil::applyTimeZoneOffset(dateTimeUTC,
DateTimeUtil::getUserPreferredTimeZone()));

// converting date/time from user time zone to utc date/time
dateTimeUTC = DateTimeUtil::newDateTime(regularDate, regularTime,
DateTimeUtil::getUserPreferredTimeZone());
}

 

For more on working with dates in Dynamics AX check out these blog posts: Working With UTC Date Time Functionality and Working With UTC Date Time Fields in Select Statements.

 

Related Posts

Recommended Reading:

Manage U.S. Use Tax on Purchase Orders in Dynamics 365 Finance and Operations

  Managing sales tax requirements on your business purchase can be complicated, but Dynamics 365 Finance and Operations can help […]

Read the Article
5.19.22 Dynamics CRM

How to Write a Great Support Ticket in the Stoneridge Support Portal

Submitting a support ticket through the Stoneridge Support Portal is a quick and effective way to get assistance for any […]

Read the Article

Managing Your Business Through Uncertain Times Using Dynamics 365 Finance and Operations

  Dynamics 365 Finance and Operations (F&O) can help you make informed decisions on how to move your business forward. […]

Read the Article
5.13.22 Power Platform

Using Power BI Object Level Security

  The following article will demonstrate how to use Power BI Object Level Security to disable column data based on […]

Read the Article
5.12.22 Dynamics CRM

How to Use the Stoneridge Support Portal

Stoneridge Software’s support portal is an intuitive and useful function that makes it easy for you to access resources to […]

Read the Article
5.6.22 Dynamics GP

Dynamics GP Transaction Removal: Purchase Orders

  Are you having performance issues with Purchase Orders?  Do you find that there are old Purchase Orders on your […]

Read the Article
5.5.22 Dynamics GP

The Real Story about the Long-Term Future of Dynamics GP Support

I’ve seen a number of people put forward comment that Dynamics GP is going away and you have to get […]

Read the Article

New Features in Dynamics 365 Business Central 2022 Wave 1 Release – Financial Enhancements

The Dynamics 365 Businses Central 2022 Wave 1 Release has a lot of new and exciting features to help your […]

Read the Article
4.29.22 Dynamics GP

Dynamics GP Transaction Removals: Bank Reconciliation

  This is part 2 of a 3 part series on Dynamics GP Transaction Removals. These quick tips will hopefully […]

Read the Article

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!

X