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

定义

返回 Date 表示指定年、月和日的值,其时间信息设置为午夜 (00:00:00) 。Returns a Date value representing the specified year, month, and day, with the time information set to midnight (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

必需。Required. 范围为 1 到 9999 的整数表达式。Integer expression from 1 through 9999. 但是,也接受小于此范围的值。However, values below this range are also accepted. 如果 Year 为0到99,则将其解释为介于1930和2029之间,如 "备注" 部分所述。If Year is 0 through 99, it is interpreted as being between 1930 and 2029, as explained in the "Remarks" section. 如果 Year 小于 1,则从当前年份减去它。If Year is less than 1, it is subtracted from the current year.

Month
Int32

必需。Required. 范围为 1 到 12 的整数表达式。Integer expression from 1 through 12. 但是,也接受此范围之外的值。However, values outside this range are also accepted. Month 值的偏移量为 1,且该值应用于所计算年份的 1 月。The value of Month is offset by 1 and applied to January of the calculated year. 换言之,就是 1 月加上 (Month - 1)。In other words, (Month - 1) is added to January. 若有必要则对年份进行重新计算。The year is recalculated if necessary. 以下的结果阐释了这种效果:The following results illustrate this effect: 如果 Month 为 1,则结果为所计算年份的 1 月。If Month is 1, the result is January of the calculated year.

如果 Month 为 0,则结果为上一年的 12 月。If Month is 0, the result is December of the previous year.

如果 Month 为 -1,则结果为上一年的 11 月。If Month is -1, the result is November of the previous year.

如果 Month 为 13,则结果为下一年的 1 月。If Month is 13, the result is January of the following year.

Day
Int32

必需。Required. 范围为 1 到 31 的 Integer 表达式。Integer expression from 1 through 31. 但是,也接受此范围之外的值。However, values outside this range are also accepted. Day 值偏移量为 1,该值应用于所计算月份的第一天。The value of Day is offset by 1 and applied to the first day of the calculated month. 换言之,就是该月份的第一天加上 (Day - 1)。In other words, (Day - 1) is added to the first of the month. 若有必要,则对月份和年份进行重新计算。The month and year are recalculated if necessary. 以下的结果阐释了这种效果:The following results illustrate this effect: 如果 Day 为 1,则结果为所计算月份的第一天。If Day is 1, the result is the first day of the calculated month.

如果 Day 为 0,则结果为上一月份的最后一天。If Day is 0, the result is the last day of the previous month.

如果 Day 为 -1,则结果为上一月份的倒数第二天。If Day is -1, the result is the penultimate day of the previous month.

如果 Day 超过当前月份的月末,则结果为下一月份中相应的一天。If Day is past the end of the current month, the result is the appropriate day of the following month. 例如,如果 Month 为 4 且 Day 为 31,则结果为 5 月 1 日。For example, if Month is 4 and Day is 31, the result is May 1.

返回

DateTime

一个值,该值表示指定的年、月和日,其时间信息设置为午夜 (00:00:00) 。A value that represents the specified year, month, and day, with the time information set to midnight (00:00:00).

示例

此示例使用 DateSerial 函数返回指定年、月、日的日期。This example uses the DateSerial function to return the date for the specified year, month, and day.

' 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 基于用户定义的计算机设置来解释。Two-digit values for the Year argument are interpreted based on user-defined computer settings. 默认设置是从0到29的值被解释为年份2000-2029,30到99的值被解释为年份1930-1999。The default settings are that values from 0 through 29 are interpreted as the years 2000-2029, and values from 30 through 99 are interpreted as the years 1930-1999. 若要表示所有其他年份,请使用四位数年份,例如1924。To signify all other years, use a four-digit year, for example, 1924.

下面的示例演示了负数、零和正值参数值。The following example demonstrates negative, zero, and positive argument values. 在此,该 DateSerial 函数返回一个, Date 它表示当前年份的第一天的第一天的第一天的第一天,即10年2月前一天的最后一天。Here, the DateSerial function returns a Date representing the day before the first day of March in the year 10 years before the current year; in other words, the last day of February ten years ago.

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

如果 MonthDay 超出其正常范围,则会根据需要将其应用到下一个更大的单位。If either Month or Day exceeds its normal range, it is applied to the next larger unit as appropriate. 例如,如果指定32天,则将其评估为一个月,一天到四天,具体取决于的值 MonthFor example, if you specify 32 days, it is evaluated as one month and from one to four days, depending on the value of Month. 如果 Year 大于9999,或者任何参数超出范围-2147483648 到2147483647,则 ArgumentException 会发生错误。If Year is greater than 9999, or if any argument is outside the range -2,147,483,648 through 2,147,483,647, an ArgumentException error occurs. 如果由三个参数指定的日期早于00:00:00 年1月1日或晚于23:59:59 年12月 9999 31 日,则 ArgumentOutOfRangeException 会发生错误。If the date specified by the three arguments is earlier than 00:00:00 on January 1 of the year 1, or later than 23:59:59 on December 31, 9999, an ArgumentOutOfRangeException error occurs.

Date数据类型包含时间组件。The Date data type includes time components. DateSerial 将所有这些值设置为0,因此返回的值表示计算出的那一天的开始值。DateSerial sets all of these to 0, so the returned value represents the beginning of the calculated day.

由于 Date 结构支持每个值 DateTime ,因此它的方法会在组合值时为您指定其他选项 DateSince every Date value is supported by a DateTime structure, its methods give you additional options in assembling a Date value. 例如,可以使用重载的构造函数之一, DateTime 使用所需的 Date 组件组合来填充变量。For example, you can use one of the overloaded DateTime constructors to populate a Date variable using the desired combination of components. 以下示例在 NewDateTime 上午8:30 之前,将设置为5月6日,1978:The following example sets NewDateTime to May 6, 1978 at one tenth of a second before 8:30 in the morning:

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

适用于

另请参阅