傳回給定日期的指定部分。
語法
DatePart(interval, date)
引數:
表格 11-11 間隔引數值
| 設定 | 描述 |
|---|---|
| yyyy | 年 |
| q | 季 |
| m | 月 |
| d | Day |
| w | 星期幾 |
| h | Hour |
| n | Minute |
| s | Second |
支援的日期格式:
MM-DD-YYYYMM/DD/YYYYDD-MM-YYYYDD/MM/YYYYYYYY-MM-DDYYYY/MM/DD支援的時間格式:
hh:mm:ss
備註
您可以使用 DatePart 函式來評估日期並傳回特定的時間間隔。例如,您可以使用 DatePart 來計算星期幾或這一小時。
這些範例採用日期並使用 DatePart 函式顯示該日期的年份季度。
範例 1:
Function GetQuarter(TheDate)
GetQuarter = DatePart("q", TheDate)
End Function
範例 2:
Dim MyDate, YearPart
MyDate = "21/01/2025"
YearPart = DatePart("yyyy", MyDate)
'Output: 2025
範例 3:
Dim MyDate, MonthPart
MyDate = "21/01/2025"
MonthPart = DatePart("m", MyDate)
'Output: MonthPart contains 1 (January)
範例 4:
Dim MyDate, DayPart
MyDate = "21/01/2025"
DayPart = DatePart("d", MyDate)
'Output: 21
範例 5:
Dim MyDate, HourPart
MyDate = "21/01/2025 14:30:00" ' Includes time component.
HourPart = DatePart("h", MyDate)
'Output: 14 (2 PM)
範例 6:
Dim MyDate, MinutePart
MyDate = "21/01/2025 14:30:00"
MinutePart = DatePart("n", MyDate)
'Output: 30
範例 7:
Dim MyDate, SecondPart
MyDate = "21/01/2025 14:30:00" ' Includes time component.
SecondPart = DatePart("s", MyDate) ' Returns the second part of the date.
'Output: 0