Access VBA 入門講座

Home日付・時刻関数日付・時刻関数

日付・時刻関数は日付、時刻データを元に指定した値を返す関数です。

書式機能
Date現在の日付を返します。
Time現在の時刻を返します。
Now現在の日付と時刻を返します。

Sub Sample

'---現在のシステム時刻を2009/01/02 12:34:56と仮定する

    MsgBox Date    '---2009/01/02を表示

    MsgBox Time    '---12:34:56を表示

    MsgBox Now    '---2009/01/02 12:34:56を表示

End Sub
						
書式機能
Year(日付)年を返します。
Month(日付)月を返します。
Day(日付)日を返します。
Hour(時刻)時を返します。
Minute(時刻)分を返します。
Second(時刻)秒を返します。

Sub Sample

'---現在のシステム時刻を2009/01/02 12:34:56と仮定する

    Dim dtNow As Date

    dtNow = Now

    MsgBox Year(dtNow)      '---2009を表示

    MsgBox Month(dtNow)     '---01を表示

    MsgBox Day(dtNow)       '---02を表示

    MsgBox Hour(dtNow)      '---12を表示

    MsgBox Minute(dtNow)    '---34を表示

    MsgBox Second(dtNow)    '---56を表示

End Sub
						
書式機能
DateSerial(年,月,日)指定した年,月,日に対応する日付を返します。
DateSerial(時,分,秒)指定した時,分,秒に対応する時刻を返します。

Sub Sample

    Dim dtDate As Date
    Dim dtTime As Date

    dtDate = DateSerial(2009,10,1)
    dtTime = TimeSerial(10,11,22)

    Msgbox dtDate    '---2009/10/01を表示

    Msgbox dtTime    '---10:11:22を表示

End Sub
						
書式DateAdd(時間間隔,数値,日付)
機能指定した日付に指定した期間を足した値を返します。
時間間隔意味
yyyy
q四半期
m
y年間通算日
d
w週日
ww
h
n
s

Sub Sample

'---現在のシステム時刻を2009/01/02 12:34:56と仮定する

    Dim dtDate As Date
    Dim dtNow As Date

    dtDate = Date

    Msgbox DateAdd("d", 3, dtDate)

    Msgbox DateAdd("m", 3, dtDate)

    Msgbox DateAdd("yyyy", 3, dtDate)

    dtNow = Now

    Msgbox DateAdd("h", 3, dtNow)

    Msgbox DateAdd("n", 3, dtNow)

    Msgbox DateAdd("s", 3, dtNow)

End Sub
						
書式DateDiff(時間間隔,日付1,日付2)
機能2つの指定した日付の時間間隔を返します。

Sub Sample

    Dim dtStartDate As Date
    Dim dtEndDate As Date

    dtStartDate = DateSerial(2009,10,1)
    dtEndDate = DateSerial(2009,11,22)

    Msgbox DateDiff("d",dtStartDate,dtEndDate)    '---52を表示

    Msgbox DateDiff("m",dtStartDate,dtEndDate)    '---1を表示

End Sub