Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

Our blog provides a curated collection of tips, tricks, and code snippets that streamline your workflow and enhance your proficiency.
Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

Some Useful Date functions in dynamics 365 Finance and Operations

0

Some Useful Date functions in dynamics 365 Finance and Operations
In Dynamics 365 Finance and Operations, date functions play a crucial role in performing various operations related to date and time manipulation within the application. These functions are used to handle and process date-related data efficiently.

These date functions are particularly useful when creating custom business logic, workflows, or reports that involve date calculations or validations. They can be used in various areas of the application, such as in X++ code, formulas, workflows, and SSRS reports.

For example, if you need to calculate the due date for an invoice by adding a specific number of days to the invoice date, you can use the dateAdd() function to achieve this efficiently.

Keep in mind that Dynamics 365 Finance and Operations may have updates and enhancements over time, so it's always a good practice to refer to the official documentation for the most up-to-date information on available date functions and their usage within the platform.

There are a lot of functions in dynamics Ax for dates. Followings are some date time functions I used extensively. Some commonly used date functions in Dynamics 365 Finance and Operations include:

1. How to get Start Date and End Date of a Month in dynamics 365 Finance and Operations

In Dynamics 365 Finance and Operations a date function DateStartMth(myDate) and endmth(myDate)  functions are used to to get Start Date and End Date of a Month from the given date in dynamics 365 finance and operations,This function returns Start Date and End Date of a Month that is used by the client.

Here i have created one runnable class DateFunctionsJob. Once you created the class , you can copy and paste the below codes as per your requirements.

Start Date of the Month

internal final class DateFunctionsJob

{
   public static void main(Args _args)
   {        Transdate dateToday;

       TransDate startDateOfMonth;

       dateToday =today();

       startDateOfMonth=DateStartMth(dateToday);

       info(strfmt("Start Date Of Month - %1",startDateOfMonth));

    }
}

Output :
Start Date Of Month - 7/1/2023

End Date of the Month

internal final class DateFunctionsJob

{    public static void main(Args _args)    {
     Transdate dateToday;

     TransDate endDateOfMonth;

      dateToday =today();

      endDateOfMonth=endmth(dateToday);

       info(strfmt("End Date Of Month - %1",endDateOfMonth));

    }

}

Output :
End Date Of Month - 7/31/2023

2. How to split datetime into hour minutes and seconds with different timezone in dynamics 365 Finance and Operations

In Dynamics 365 Finance and Operations a date functions DateTimeUtil::hour(myCurrentDateTime), DateTimeUtil::minute (myCurrentDateTime);and DateTimeUtil::second(myCurrentDateTime); are used to tto split datetime into hour minutes and seconds with different timezone from the given datetime in dynamics 365 finance and operations,This function returns to split datetime into hour minutes and seconds with different timezone that is used by the client.

internal final class DateFunctionsJob

{
   public static void main(Args _args)
   {
     TransDateTime myCurrentDateTime;

    int      hours;

    int      minutes;

    int      seconds;

myCurrentDateTime=DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(),Timezone::GMTPLUS0530CHENNAI_KOLKATA_MUMBAI);

hours=DateTimeUtil::hour(myCurrentDateTime);

minutes=DateTimeUtil::minute(myCurrentDateTime);

seconds=DateTimeUtil::second(myCurrentDateTime);

info(strfmt('Current Date Time - %1 ',datetime2str(myCurrentDateTime)));

 info(strfmt('Hours %1 - Minutes %2 - Seconds %3',int2str(hours),int2str(minutes),int2str(seconds)));

    }
}

Output:
Current Date Time - 7/23/2023 12:00:03 pm

Hours 12 - Minutes 0 - Seconds 3

Here you can change the time zone by selecting the timezone in the code myCurrentDateTime=DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(),Timezone::GMTPLUS0530CHENNAI_KOLKATA_MUMBAI);

3. How to get current system Time in Dynamics 365 Finance and Operations?

In Dynamics 365 Finance and Operations a date function timenow() is used to get the current system time in dynamics 365 finance and operations,This function returns the current time that is used by the client.

internal final class DateFunctionsJob

{
   public static void main(Args _args)
   {
     info(strfmt('Current System Time - %1',time2str(timenow(),1,1)));

    }
}

Output:
Current System Time - 07:01:29

4. How to get current system Date with timestamp in dynamics 365 Finance and Operations

In Dynamics 365 Finance and Operations a date function DateTimeUtil::getSystemDateTime(); is used to get current system DateTime (with hours, minutes, seconds) in dynamics 365 finance and operations,This function returns the current system DateTime (with hours, minutes, seconds) that is used by the client.

internal final class DateFunctionsJob

{
   public static void main(Args _args)
   {

       utcDateTime   mySystemDateTime = DateTimeUtil::getSystemDateTime();

       info(strfmt('Current System Date with Time Stamp - %1',mySystemDateTime));

    }
}

Output:
Current System Date with Time Stamp - 7/23/2023 09:46:55 am

5. Convert UtcDateTime to string in dynamics 365 Finance and Operations

In Dynamics 365 Finance and Operations a function DateTimeUtil::toStr(myDateTime) ; is used to Convert UtcDateTime to string in dynamics 365 finance and operations,This function returns the converted UtcDateTime to string that is used by the client.

internal final class DateFunctionsJob

{
   public static void main(Args _args)
   {

        utcDateTime   mySystemDateTime = DateTimeUtil::getSystemDateTime();       

str myStringSystemDateTime= DateTimeUtil::toStr(mySystemDateTime);   

        info(strfmt('String UtcDateTime is - %1 ',myStringSystemDateTime));

    }
}

Output:
String UtcDateTime is - 2023-07-23T09:56:33

6. Convert datatime to string into a specific format in dynamics 365 Finance and Operations

In Dynamics 365 Finance and Operations a function DateTimeUtil::toStr(myDateTime) ; is used to Convert datatime to string into a specific format in dynamics 365 finance and operations,This function returns the converted datatime to string into a specific format that is used by the client.

internal final class DateFunctionsJob

{
   public static void main(Args _args)
   {

       System.DateTime myDateTime = System.DateTime::get_UtcNow(); 

       str utcTimeAsStr =myDateTime.ToString('yyyy-M-dd_HH:mm:ss');   

       info(strFmt("String Datetime with New Format : %1 ",utcTimeAsStr));

    }
}

Output:
String Datetime with New Format: 2023-7-23_10:26:58

in the output result you can see the new date format , you can change the format as per the requirements.

7. How to check if a utcDateTime is Null in dynamics 365 Finance and Operations using X++

In Dynamics 365 Finance and Operations you can use the condition if(MyUtcDateTime== Global::utcDateTimeNull()) to check if a utcDateTime is Null in X++.

internal final class DateFunctionsJob

{
   public static void main(Args _args)
   {

       utcdatetime MyUtcDateTime;

       if(MyUtcDateTime== Global::utcDateTimeNull())

       {

           info(strFmt("MyUtcDateTime has Null value "));

       }

    }
}

Conclusion

First and foremost, Dynamics 365 date functions significantly enhance the efficiency and accuracy of date-related calculations. Tasks such as adding or subtracting days, months, or years, determining the difference between two dates, or extracting specific components (e.g., day, month, year) from a date become seamless and error-free with the help of these functions. By automating these processes, organizations can save valuable time and resources, allowing their teams to focus on more strategic and value-added activities.

Tags

X++ date runtime functions - Dynamics 365 Finance and Operations
Date and Time Functions in Dynamics 365 Finance and Operations
Using the Date Function in Microsoft Dynamics 365 Finance and Operations
Date functions in dynamics 365 Finance and Operations
Date functions in dynamics 365 FO

Post a Comment

0Comments
Post a Comment (0)