Examples
DateSerial (1999, 6, 15)
DateSerial (2000, 1 - 7, 15)
DateSerial (1999, 1, 166)
All three return the date June 15, 1999. The second says that 7 months before January 1, 2000 is June 15, 1999. The third says that the 166th day of 1999 is June 15, 1999.
DateSerial (1996 + 12, 2 + 13, 29 + 14)
Returns April 12, 2009. What it means is that 12 years, 13 months and 14 days from February 29, 1996 is April 12, 2009.
Suppose you want to calculate the last day of the month for the DateTime field {Orders.Order Date}. Notice that in the calculation, DateSerial(Year(d), Month(d) + 1, 1) is the first day of the month after {Orders.Order Date} and then subtracting one day gives the desired result:
Local DateTimeVar d := {Orders.Order Date};
DateSerial(Year(d), Month(d) + 1, 1 - 1)
It is often useful to combine DateSerial with other date functions. For example, if you want to calculate the date of the last Friday of the month for the field {Orders.Order Date} you can do the following.
The calculation proceeds by finding the last day of the month and then subtracting off a number of days to get to a Friday. Since some months have 5 Fridays (example: October 1999) and some have 4 Fridays (example: November 1999), this approach is easier than working from the first day of the month.
Local DateTimeVar d1 := {Orders.Order Date};
Local DateVar d2;
d2 := DateSerial(Year(d1), Month(d1) + 1, 1 - 1);
d2 - (DayOfWeek(d2, crFriday) - 1)
Returns the Date value March 27, 1998 if {Orders.Order Date} is March 18, 1998.
Here is an example that returns the date of the second Tuesday of the month 3 months before {Orders.Order Date}:
Local DateTimeVar d1 := {Orders.Order Date};
Local DateVar d2 := DateSerial(Year(d1), Month(d1) - 3, 1);
d2 + (2 * 7 - (DayOfWeek(d2, crTuesday) - 1))
Returns the Date value December 9, 1997 if {Orders.Order Date} is March 18, 1998.