title | description | author | ms.author | ms.topic | ms.date | ms.reviewer | audience | ms.search.region | ms.search.validFrom | ms.dyn365.ops.version |
---|---|---|---|---|---|---|---|---|---|---|
X++ date runtime functions |
Learn about the date run-time functions, including the dyName, dayOfMth, dayOfWk, and dayOfYr functions, with parameters, return values, and examples. |
josaw1 |
josaw |
article |
11/03/2017 |
johnmichalak |
Developer |
Global |
2016-02-28 |
AX 7.0.0 |
[!include banner]
This article describes the date run-time functions.
Retrieves the name of the day of the week that is specified by a number.
str dayName(int number)
Parameter | Description |
---|---|
number | The number of a day in a week. |
The day of the week specified by the number parameter.
The valid values for the number parameter are 1 through 7. Monday is represented by 1, Tuesday by 2, and Sunday by 7.
static void dayNameExample(Args _arg)
{
str s;
;
s = dayName(01);
print "First day of the week's name is " + s;
pause;
}
Calculates the number of the day in the month for the specified date.
int dayOfMth(date date)
Parameter | Description |
---|---|
date | The date to test. |
An integer between 1 and 31 that indicates the day of the month for the specified date.
dayOfMth(31122001) //returns 31.
static void dayOfMthExample(Args _arg)
{
date d = today();
int i;
;
i = dayOfMth(d);
print "Today's day of the month is " + int2Str(i);
pause;
}
Calculates the number of day in the week for the specified date. Note: Monday is represented by 1, Tuesday by 2, and Sunday by 7.
int dayOfWk(date date)
Parameter | Description |
---|---|
date | A date value that indicates the year, month, and day. |
The number of the specified day in the week.
static void dayOfWkExample(Args _arg)
{
date d = today();
int i;
;
i = dayOfWk(d);
print "Today's day of the week is " + int2Str(i);
pause;
}
Calculates the number of days between January 1 and the specified date.
int dayOfYr(date _date)
Parameter | Description |
---|---|
_date | A date that specifies the year, month, and day. |
The number of days between January 1 and the specified date, inclusive.
January 1 is 1, and December 31 is either 365 or 366.
static void dayOfYrExample(Args _arg)
{
date d = today();
int i;
;
i = dayOfYr(d);
print "Today's day of the year is " + int2Str(i);
pause;
}
Calculates the last date in the month of the specified date.
date endMth(date date)
Parameter | Description |
---|---|
date | A date value that indicates a year, month, and day. |
The date value of the last day in the specified month.
endMth(0221988); //Returns the date 2921988 because 1988 is a leap year.
endMth(0221989); //Returns the date 2821989.
Creates a date, based on three integers that indicate the day, month, and year, respectively. "Shorthand" values for the year argument, for example, "y", are not supported.
date mkDate(int day, int month, int year)
Parameter | Description |
---|---|
day | An integer that represents the day of the month. |
month | An integer that represents the month of the year. |
year | An integer that represents the year, which must be between 1900 and 2154. |
A date value that is based on the values of the day, month, and year parameters.
If the date isn't valid, this method returns a 0 (zero, 1/1/1900) date. Beginning with Dynamics AX 7.0(February 2016), shortcut values for the year, e.g. 75 for 1975, are not supported. If you provide a shortcut value for the year, a date of 1/1/1900 is returned.
static void mkDateExample(Args _arg)
{
date d;
;
// Returns the date 0112005.
d = mkDate(1, 1, 2005);
print d;
pause;
}
Retrieves the name of the specified month
str monthName(int number)
Parameter | Description |
---|---|
number | The number of the month. |
The name of the specified month.
The valid values of the number parameter are 1 through 12. January is represented by 1 and December by 12.
static void mthNameExample(Args _arg)
{
str s;
;
// MthName(6) returns the text string "June".
s = mthName(6);
print "Month name is " + s;
pause;
}
Retrieves the number of the month in the year for the specified date. Note: January is 1, February is 2, and December is 12.
int mthOfYr(date date)
Parameter | Description |
---|---|
date | A date that specifies a year, month, and day. |
The number of the month in the year, for the month that is represented by the date parameter.
static void mthOfYrExample(Args _arg)
{
int i;
;
i = mthOfYr(today());
print "The number of the month in today's date is " + int2Str(i);
pause;
}
Retrieves the date in the following month that corresponds most closely to the specified date.
date nextMth(date date)
Parameter | Description |
---|---|
date | The date to match in the following month. |
The closest match to the specified date that is found in the next month.
nextMth(2921996); //returns 29/03/1996.
nextMth(3111996); //returns 2921996, because 1996 is a leap year.
static void nextMthExample(Args _arg)
{
date d;
;
d = nextMth(today());
print "Closest date next month is "
+ date2Str(d, 2, 2, -1, 2, -1, 4);
pause;
}
Retrieves the date in the following quarter that corresponds most closely to the specified date.
date nextQtr(date date)
Parameter | Description |
---|---|
date | The date to match in the following quarter. |
The closest match to specified date that is found in the next quarter.
For example, nextQtr(3111998) returns 3041998.
static void nextQtrExample(Args _arg)
{
date d;
;
d = nextQtr(today());
print "Closest date next quarter is "
+ date2Str(d, 2, 2, -1, 2, -1, 4);
pause;
}
Retrieves the date in the following year that corresponds most closely to the specified date.
date nextYr(date date)
Parameter | Description |
---|---|
date | The date to match in the following year. |
The closest match to the specified date that is found in the following year.
For example, nextyr(2921998) returns 2821999.
static void nextYrExample(Args _arg)
{
date d;
;
d = nextYr(today());
print "Closest date next year is "
+ date2Str(d, 2, 2, -1, 2, -1, 4);
pause;
}
Retrieves the date in the previous month that corresponds most closely to the specified date.
date prevMth(date date)
Parameter | Description |
---|---|
date | The date to match in the previous month. |
The closest match to the specified date that is found in the previous month.
prevMth(3131996); //Returns the date 29/02/1996 because 1996 is a leap year.
prevMth(2821998); //Returns the date 28/01/1998.
Retrieves the date in the previous quarter that corresponds most closely to the specified date.
date prevQtr(date date)
Parameter | Description |
---|---|
date | The date to match in the previous quarter. |
The closest match to the specified date that is found in the previous quarter.
prevQtr(3041998); //Returns the date 30/01/1998.
prevQtr(2951996); //Returns the date 29/02/1996, because 1996 is a leap year.
Retrieves the date in the previous year that corresponds most closely to the specified date.
date prevYr(date date)
Parameter | Description |
---|---|
date | The date to match in the previous year. |
The closest match to the specified date that is found in the previous year.
prevYr(2921996); //Returns the date 28/02/1995 because 1996 is a leap year.
prevYr(2821998); //Returns the date 28/02/1997.
Retrieves the session date, if it has been set.
date systemDateGet()
The session date if it has been set; otherwise, the system date.
Consider using Session date and time on the Tools menu to open the Session date and time page. This page can be used to actively set the session date. After this set action is detected by the system, subsequent calls to the systemDateGet function return the session date. The today function returns the system date. This function doesn't support time zones.
The following example shows the date in the Infolog window.
static void Job_systemDateGet(Args _arg)
{
info( date2Str(
systemDateGet(), // X++ language function.
321, // 321 = ymd
DateDay::Digits2,
DateSeparator::Hyphen, // separator1
DateMonth::Digits2,
DateSeparator::Hyphen, // separator2
DateYear::Digits4
)
);
/*********** Actual Infolog output
Message (03:46:00 pm)
2012-04-16
***********/
}
Changes the system date.
date systemDateSet(date _date)
Parameter | Description |
---|---|
_date | The new date for the system. |
The new system date.
This function doesn't affect the session date. This method changes the date, but the time will be set to 0 (zero).
The following example sets the system date to today's date.
static void systemDateSetExample(Args _arg)
{
date d = today();
d = systemDateSet(d);
print d;
}
Retrieves the current system time.
int timeNow()
The number of seconds that have passed since midnight.
static void timeNowExample(Args _arg)
{
int i;
;
i = timeNow();
print "The number of seconds since midnight is " + int2Str(i);
pause;
}
Retrieves the current date on the system.
date today()
The current date.
static void todayExample(Args _arg)
{
date d;
;
d = today();
print "Today's date is " + date2Str(d, 0, 2, -1, 2, -1, 4);
pause;
}
Calculates the week of the year that a date falls in, according to the ISO 8601 specification.
int wkOfYr(date _date)
Parameter | Description |
---|---|
_date | The date to calculate the week of the year for. |
The sequence number of the week that the _date parameter occurs in.
The following code example compares the wkOfYr function with the Global::weekOfYear method. The function and the method produce different results.
// X++ job, under AOT > Jobs.
static void WeekTests3Job(Args _args)
{
int weekNum, i;
date dateTest;
str sMessages[];
//---------------------------------------------
sMessages[1] = "----- #1. For Sunday, January 5, 2003 -----";
dateTest = 512003; // DayMonthYear format.
weekNum = wkOfYr(dateTest);
sMessages[2] = int2str(weekNum) + " = wkOfYr funtion";
weekNum = Global::weekOfYear(dateTest);
sMessages[3] = int2str(weekNum) + " = Global::weekOfYear method";
//---------------------------------------------
sMessages[4] = " ";
sMessages[5] = "----- #2. For Wednesday, August 20, 2003 -----";
dateTest = 2082003;
weekNum = wkOfYr(dateTest);
sMessages[6] = int2str(weekNum) + " = wkOfYr funtion";
weekNum = Global::weekOfYear(dateTest);
sMessages[7] = int2str(weekNum) + " = Global::weekOfYear method";
//---------------------------------------------
sMessages[8] = " ";
sMessages[9] = "----- #3. For Sunday, December 28, 2003 -----";
dateTest = 28122003;
weekNum = wkOfYr(dateTest);
sMessages[10] = int2str(weekNum) + " = wkOfYr funtion";
weekNum = Global::weekOfYear(dateTest);
sMessages[11] = int2str(weekNum) + " = Global::weekOfYear method";
for (i=1; i<= 11; i++)
{
Global::info(sMessages[i]);
}
}
The previous example sent the following information to the Infolog for display. The output shows that there are differences between wkOfYr and Global::weekOfYear.
Message (01:59:13 pm) -----
#1. For Sunday, January 5, 2003 ----- 1 = wkOfYr function 2 = Global::weekOfYear method -----
#2. For Wednesday, August 20, 2003 ----- 34 = wkOfYr function 34 = Global::weekOfYear method -----
#3. For Sunday, December 28, 2003 ----- 52 = wkOfYr function 1 = Global::weekOfYear method
Retrieves the year from a date value.
int year(date _date)
Parameter | Description |
---|---|
_date | The date to return the year from. |
The year of the specified date.
year(0221998); //Returns the value 1998.
[!INCLUDEfooter-include]