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,則結果為前一年十一月。

Month 為13,則結果為隔年一月。

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 年三月一日前的一天;換言之,十年前二月的最後一天。

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

若任一 MonthDay 超過正常範圍,則會依適當方式套用到下一個較大的單位。 例如,若指定 32 天,則評估為一個月,且根據 的 Month值從一至四天不等。 若 Year 大於 9999,或任何參數位於 -2,147,483,648 至 2,147,483,647 之間,則會發生 ArgumentException 錯誤。 若三個論點指定的日期早於第1年1月1日的00:00:00,或晚於9999年12月31日的23:59:59,則 ArgumentOutOfRangeException 會發生錯誤。

資料型態 Date 包含時間成分。 DateSerial 將這些全部設為 0,因此回傳的值代表計算當天的開始。

由於每個 Date 值都有 DateTime 結構支撐,其方法為組合值提供了更多選項 Date 。 例如,你可以使用超 DateTime 載的建構子,根據想要的組件組合來填充變 Date 數。 以下範例設定 NewDateTime 在1978年5月6日上午8:30前0.0秒:

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

適用於

另請參閱