### DateSerial (year, month, day)

Basic and Crystal syntax.

##### Arguments

- year is a whole Number or numeric expression representing a year, example: 1996.
- month is a whole Number or numeric expression representing a month, example: 12 for December.
- day is a whole Number or numeric expression representing a day of the month, example: 5.

##### Returns

A Date value.

##### Action

DateSerial returns a Date value for the specified year, month and day. It also handles relative Date expressions.

##### Typical uses

DateSerial can be used instead of CDate or DateValue to create a Date value out of a year, month and day.

A useful feature of DateSerial is that the month argument does not have to be from 1 to 12 and the day argument does not have to be in the valid range for the number of days in the month. Such dates are interpreted as relative dates, and DateSerial will produce a valid Date value. This can be used to perform many computations with dates, without checking for special cases such as the end of the year, leap years and the number of days in a month. See the examples below for some typical applications.

##### Examples

The following examples are applicable to both Basic and Crystal syntax:

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:

Rem Basic syntax
Dim d
d = {Orders.Order Date}
formula = DateSerial(Year(d), Month(d) + 1, 1 - 1)
//Crystal syntax
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.

Rem Basic syntax
Dim d1, d2
d1 = {Orders.Order Date}
d2 = DateSerial(Year(d1), Month(d1) + 1, 1 - 1)
formula = d2 - (WeekDay(d2, crFriday) - 1)
//Crystal syntax
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}:

Rem Basic syntax
Dim d1, d2
d1 = {Orders.Order Date}
d2 = DateSerial(Year(d1), Month(d1) - 3, 1)
formula = d2 + (2 * 7 - (WeekDay(d2, crTuesday) - 1))
//Crystal syntax
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.

##### Comment

This function is designed to work like the Visual Basic function of the same name.

Seagate Software, Inc. http://www.seagatesoftware.com Please send comments to: techpubs@seagatesoftware.com