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?
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
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
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
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++
internal final class DateFunctionsJob
{
public static void main(Args _args)
{
utcdatetime MyUtcDateTime;
if(MyUtcDateTime== Global::utcDateTimeNull())
{
info(strFmt("MyUtcDateTime has Null value "));
}
}
}
0 Comments