DateAndTime.TimeSerial(Int32, Int32, Int32) 方法
定义
返回一个 Date 值,该值表示指定的小时、分钟和秒,其日期信息设置基点为元年 1 月 1 日。Returns a Date value representing a specified hour, minute, and second, with the date information set relative to January 1 of the year 1.
public:
static DateTime TimeSerial(int Hour, int Minute, int Second);
public static DateTime TimeSerial (int Hour, int Minute, int Second);
static member TimeSerial : int * int * int -> DateTime
Public Function TimeSerial (Hour As Integer, Minute As Integer, Second As Integer) As DateTime
参数
- Hour
- Int32
必需。Required. 范围为 0 到 23 的整数表达式。Integer expression from 0 through 23. 但是,也接受此范围之外的值。However, values outside this range are also accepted.
- Minute
- Int32
必需。Required. 范围为 0 到 59 的整数表达式。Integer expression from 0 through 59. 但是,也接受此范围之外的值。However, values outside this range are also accepted. Minute 的值加入所计算的小时,因此负值指定该小时前的某几分钟。The value of Minute is added to the calculated hour, so a negative value specifies minutes before that hour.
- Second
- Int32
必需。Required. 范围为 0 到 59 的整数表达式。Integer expression from 0 through 59. 但是,也接受此范围之外的值。However, values outside this range are also accepted. Second 的值加入所计算的分钟,因此负值指定该分钟前的某几秒。The value of Second is added to the calculated minute, so a negative value specifies seconds before that minute.
返回
一个 Date 值,该值表示指定的小时、分钟和秒,其日期信息设置基点为元年 1 月 1 日。A Date value representing a specified hour, minute, and second, with the date information set relative to January 1 of the year 1.
例外
-2,147,483,648 到 2,147,483,647 范围以外的参数。An argument is outside the range -2,147,483,648 through 2,147,483,647
计算所得的时间小于负 24 小时。Calculated time is less than negative 24 hours.
示例
下面的示例使用 TimeSerial 函数返回指定的小时、分钟和秒的时间。The following example uses the TimeSerial function to return a time for the specified hour, minute, and second.
Dim thisTime As Date
thisTime = TimeSerial(16, 35, 17)
注解
下面的示例演示了负数、零和正值参数值。The following example demonstrates negative, zero, and positive argument values. 该 TimeSerial 函数返回一个时间,该时间表示在中午之前的三个小时之前的15分钟,或是 8:45:00 AM。The TimeSerial function returns a time representing 15 minutes before three hours before noon, or 8:45:00 AM.
Dim alarmTime As Date = TimeSerial(12 - 3, -15, 0)
如果 Minute 或 Second 超出其正常范围,则会根据需要将其应用到下一个更大的单位。If either Minute or Second exceeds its normal range, it is applied to the next larger unit as appropriate. 例如,如果指定75分钟,则计算结果为1小时15分钟。For example, if you specify 75 minutes, it is evaluated as one hour and 15 minutes.
TimeSerial 缩短总秒数86400,即一天中的秒数。TimeSerial reduces the total seconds modulo 86,400, which is the number of seconds in a day. 因此,返回的时间始终介于00:00:00 和23:59:59 之间。Therefore, the returned time is always between 00:00:00 and 23:59:59.
Date数据类型包括日期部分。The Date data type includes date components. TimeSerial 将所有这些都设置为1,因此返回的值表示第1年的第一天。TimeSerial sets all of these to 1, so the returned value represents the first day of the year 1. 但是,如果参数的值导致计算出的时间超过24小时,则该日期将根据需要递增。However, if the values of the arguments cause the calculated time to exceed 24 hours, the day is incremented as necessary. 在下面的示例中,和的 Hour 值 Minute 导致组合时间超过24小时。In the following example, the values of Hour and Minute result in a combined time of more than 24 hours.
MsgBox(TimeSerial(23, 75, 0))
' The preceding statement displays "1/2/0001 12:15:00 AM".
如果参数的值导致计算出的时间为负,则日期信息将设置为1/1/0001,并且时间信息将调整为介于00:00:00 和23:59:59 之间。If the values of the arguments result in a negative calculated time, the date information is set to 1/1/0001 and the time information is adjusted to be between 00:00:00 and 23:59:59. 但是,如果计算出的时间小于负24小时,则 ArgumentOutOfRangeException 会发生错误。However, if the calculated time is less than negative 24 hours, an ArgumentOutOfRangeException error occurs.
由于 Date 结构支持每个值 System.DateTime ,因此它的方法会在组合值时为您指定其他选项 Date 。Since every Date value is supported by a System.DateTime structure, its methods give you additional options in assembling a Date value. 例如,你可以使用重载的 DateTime 构造函数之一来 Date 使用所需的组件组合来填充变量。For example, you can employ 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)