DateAndTime.DateSerial(Int32, Int32, Int32) 方法

定义

返回一个 Date 值,该值表示指定的年份、月和日,时间信息设置为午夜(00:00:00)。

public:
 static DateTime DateSerial(int Year, int Month, int Day);
public static DateTime DateSerial(int Year, int Month, int Day);
static member DateSerial : int * int * int -> DateTime
Public Function DateSerial (Year As Integer, Month As Integer, Day As Integer) As DateTime

参数

Year
Int32

必填。 从 1 到 9999 的整数表达式。 但是,也接受此范围下面的值。 如果 Year 为 0 到 99,则解释为 1930 年至 2029 年间,如“备注”部分中所述。 如果 Year 小于 1,则从当前年份减去。

Month
Int32

必填。 从 1 到 12 的整数表达式。 但是,也接受此范围之外的值。 值 Month 偏移量为 1,并应用于计算年份的 1 月。 换句话说,(Month - 1)将添加到一月份。 如有必要,将重新计算年份。 以下结果说明了此效果:

如果 Month 为 1,则结果为计算年份的 1 月。

如果 Month 为 0,则结果为上一年的 12 月。

如果 Month 为 -1,则结果为上一年的 11 月。

如果 Month 为 13,则结果为次年 1 月。

Day
Int32

必填。 从 1 到 31 的整数表达式。 但是,也接受此范围之外的值。 该值 Day 偏移 1,并应用于计算月份的第一天。 换句话说,(Day - 1)将添加到本月的第一个月。 如有必要,将重新计算月份和年份。 以下结果说明了此效果:

如果 Day 为 1,则结果为计算月份的第一天。

如果 Day 为 0,则结果为前一个月的最后一天。

如果 Day 为 -1,则结果是前一个月的倒数第二天。

如果 Day 超过本月的结束时间,则结果是下个月的相应日期。 例如,如果 Month 为 4 且 Day 为 31,则结果为 5 月 1 日。

返回

一个值,该值表示指定的年份、月和日,时间信息设置为午夜(00:00:00)。

示例

此示例使用 DateSerial 函数返回指定年份、月和日的日期。

' DateSerial returns the date for a specified year, month, and day.
Dim aDate As Date
' Variable aDate contains the date for February 12, 1969.
aDate = DateSerial(1969, 2, 12)
Console.WriteLine(aDate)

' The following example uses DateSerial to determine and display
' the last day of the previous month.
' First, establish a starting date.
Dim startDate = #1/23/1994#
' The 0 for the day represents the last day of the previous month.
Dim endOfLastMonth = DateSerial(startDate.Year, startDate.Month, 0)
Console.WriteLine("Last day in the previous month: " & endOfLastMonth)

' The following example finds and displays the day of the week that the 
' 15th day of the following month will fall on.
Dim fifteenthsDay = DateSerial(Today.Year, Today.Month + 1, 15)
Console.WriteLine("The 15th of next month is a {0}", fifteenthsDay.DayOfWeek)

注解

根据用户定义的计算机设置解释参数的 Year 两位数值。 默认设置是,从 0 到 29 的值被解释为 2000-2029 年,30 到 99 的值被解释为 1930-1999 年。 若要表示所有其他年份,请使用四位数年份,例如 1924 年。

以下示例演示负、零和正自变量值。 在这里,该 DateSerial 函数返回一个 Date 表示在当前年份之前的 10 年 3 月的第一天之前的一天;换句话说,即 20 年前的最后一天。

Dim EndFeb As Date = DateSerial(-10, 3, 0)

如果任一 MonthDay 超出其正常范围,则会根据需要将其应用到下一个更大的单位。 例如,如果指定 32 天,则计算结果为一个月,从一到四天,具体取决于值 Month。 如果 Year 大于 9999,或者任何参数超出范围 -2,147,483,648 到 2,147,483,647,647, ArgumentException 则会发生错误。 如果三个参数指定的日期早于 1 年 1 月 1 日的 00:00:00:00,或晚于 9999 年 12 月 31 日 23:59:59,则 ArgumentOutOfRangeException 会发生错误。

数据类型 Date 包括时间组件。 DateSerial 将所有这些值设置为 0,因此返回的值表示计算日期的开头。

由于结构支持DateTime每个Date值,因此其方法提供了用于组合Date值的其他选项。 例如,可以使用其中一个重载 DateTime 构造函数来使用所需的组件组合填充 Date 变量。 以下示例将 1978 年 5 月 6 日设置为 NewDateTime 上午 8:30 之前第 10 秒的十分之一:

Dim NewDateTime As Date = New Date(1978, 5, 6, 8, 29, 59, 900)

适用于

另请参阅