针对指定年、月、日返回一个 Date 子类型的变体。
语法
DateSerial(year, month, day)
参数:
注释
要指定日期(例如 1991 年 12 月 31 日),每个 DateSerial 参数的数字范围都应在相应单位的可接受范围内,即 1-31 代表日,1-12 代表月。但是,您还可以使用任何数字表达式为每个参数指定相对日期,表示早于或晚于特定日期的天数、月数或年数。
以下示例使用数字表达式,而不是绝对日期数字。在此示例中,DateSerial 函数返回的是以下日期:1990 年往前推 10 年(1990 - 10)、8 月往前推两个月(8 - 2)、该月第 1 天往前推 1 天(1 - 1)所对应的日期;换言之,即 1980 年 5 月 31 日。
Dim MyDate1, MyDate2 MyDate1 = DateSerial(1970, 1, 1) Output: 01-Jan-70 MyDate2 = DateSerial(1990 - 10, 8 - 2, 1 - 1) 'Output: 31-May-80
对于年份参数,介于 0 和 99 之间的值(含 0 和 99)将被解释为 1900–1999 年。对于所有其他年份参数,请使用完整的四位数年份(例如 1800)。
如果任何参数超出相应参数的可接受范围,将会根据需要递进到下一个更大的单位。例如,如果指定 35 天,则会根据所应用于年份中的月份,计算为一个月零若干天。但是,如果任何单个参数超出 -32,768 到 32,767 的范围,或者如果三个参数(直接或通过表达式)指定的日期超出可接受的日期范围,则会出现错误。
输出日期格式基于系统的短日期格式。
以下示例说明了 DateSerial 函数的用法:
示例 1:
Dim MyDate1 MyDate1 = DateSerial(1970, 1, 1) 'Output: 01-Jan-70
示例 2:
Dim MyDate2 MyDate2 = DateSerial(1990 - 10, 8 - 2, 1 - 1) 'Output: 31-May-80
示例 3:
Dim MyDate3 MyDate3 = DateSerial(2025, 15, 40) 'Output: 09-Apr-26
示例 4:
Dim MyDate4 MyDate4 = DateSerial(2025, 1, 0) 'Output: 31-Dec-24
示例 5:
Dim MyDate5 MyDate5 = DateSerial(50, 1, 1) 'Output: 01-Jan-50
示例 6:
Dim MyDate6 MyDate6 = DateSerial(99, 12, 31) 'Output: 31-Dec-99