MakeDate() Function

MakeDate( nMonth, nDay, nYear, [lLimitToLastDOM] )

MakeDate is a date function with tremendous flexibility. In its simplest form it creates a date value from a given month, day and year, for example:

  • MakeDate( 1, 1, 2006 ) => 1/1/2006

  • MakeDate( 3, 13, 1987 ) => 3/13/1987

It may seem (and in fact is) silly to use this expression to refer to a known date.

The power and flexibility of the function come from the range of possibilities for the parameters, especially when they come from a variable or a field value that changes as a report or query moves to different records.

Here's how you can use the MakeDate function return "the first day of next month":

MakeDate(MONTH(Today)+1, 1, YEAR(Today))

The real key to this function is that the nMonth and nDay values can be "out of range", thus calculating a different month or year. For example, you would expect nDay to be limited to values between 1 and 31(and less on shorter months). But, at the time you use the function in an expression you don't know which month is currently accessed; the function needs to work anyway. So nDay can in fact be any integer number, negative or positive (or even zero!). Since the last day of last month is one day less than the first day of this month, the calculation is this easy:

MakeDate( MONTH(Today),0, YEAR(Today) )

because zero is one day prior to day 1 of this month. Passing -1 would give the second-to-last day of the prior month.

Similarly, the nMonth parameter can be "out of range". Since month 1 is January, month 0 would be December of the prior year:

MakeDate( 0, 25, 2006 ) => 12/25/2005

and a realistic usage would be:

MakeDate( MONTH(Today) - 1, 1, YEAR(Today) )

which in January would yield the previous December 1st, and in February would yield January 1st.

Values can also be out of range to the future. If nDay is 32 you will get the following month with a day number that varies with the number of days in this month. This is an unusual example, but in cases where you have an incrementing counter it could look like:

MakeDate( MONTH(Today), nCounter, YEAR(Today) )

and you could be seeing each date in succession.

For the nMonth parameter, passing a larger value moves the result into the following year (notice 2007):

MakeDate( 13, 25, 2006 ) => 12/25/2007

A fourth optional parameter is seldom used. If you pass .T. it will limit the resulting date to the last day of the given month. For example: if nDay is 31 and nMonth is 2 (February), the result will be limited to February 28th or 29th depending on if it's a leap year.

MakeDate( 2, 31, 2005, .T. ) => 2/28/2005

MakeDate( 2, 31, 2008, .T. ) => 2/29/2008

Without the fourth parameter, the nDay value is interpreted to roll forward into the next month as previously described.