日付や時刻を取得する処理、日付や時刻を設定して計算を行う処理をまとめて記載してあります。
Sub Sample5()
Dim date_time As Date
Dim output_date_time As Date
' 今日の日付
date_time = Now
MsgBox date_time ' 2024/08/19 12:34:56
date_time = Date
MsgBox date_time ' 2024/08/19
date_time = Time
MsgBox date_time ' 12:34:56
date_time = Now
MsgBox date_time ' 2024/08/19 12:34:56
date_time = Date
MsgBox Format(Date, "Long Date") ' 2024年8月19日
' 今日の日付を分解
date_time = Now
MsgBox Year(date_time) ' 2024
MsgBox Month(date_time) ' 8
MsgBox Day(date_time) ' 19
MsgBox Hour(date_time) ' 12
MsgBox Minute(date_time) ' 34
MsgBox Second(date_time) ' 56
' 和暦で表示
date_time = Now
MsgBox Format(date_time, "gee") ' R06
MsgBox Format(date_time, "ggg ee 年") ' 令和 6 年
' 曜日で表示
date_time = Now
MsgBox Weekday(date_time) ' 2 (1:日,2:月,3:火,4:水,5:木,6:金,7:土)
MsgBox WeekdayName(Weekday(date_time)) ' 月曜日
' 数値で指定
date_time = DateSerial(2024, 8, 19)
MsgBox date_time ' 2024/08/19
date_time = TimeSerial(12, 34, 56)
MsgBox date_time ' 12:34:56
date_time = DateSerial(2024, 8, 19) + TimeSerial(12, 34, 56)
MsgBox date_time ' 2024/08/19 12:34:56
' 文字列で指定
date_time = CDate("2024/8/19")
MsgBox date_time ' 2024/08/19
date_time = CDate("12:34:56")
MsgBox date_time ' 12:34:56
date_time = CDate("2024/8/19 12:34:56")
MsgBox date_time ' 2024/08/19 12:34:56
' 今月末
date_time = Now
output_date_time = WorksheetFunction.EoMonth(date_time, 0)
MsgBox output_date_time
' 先月末
date_time = Now
output_date_time = WorksheetFunction.EoMonth(date_time, -1)
MsgBox output_date_time
' 来月末
date_time = Now
output_date_time = WorksheetFunction.EoMonth(date_time, 1)
MsgBox output_date_time
' 今月頭
date_time = Now
output_date_time = DateSerial(Year(date_time), Month(date_time), 1)
MsgBox output_date_time
' 先月頭
date_time = Now
output_date_time = DateSerial(Year(date_time), Month(date_time) - 1, 1)
MsgBox output_date_time
' 先々月頭
date_time = Now
date_time = CDate("2024/1/5")
output_date_time = DateSerial(Year(date_time), Month(date_time) - 2, 1)
MsgBox output_date_time
' 来月頭
date_time = Now
output_date_time = DateSerial(Year(date_time), Month(date_time) + 1, 1)
MsgBox output_date_time
' 来々月頭
date_time = CDate("2024/12/5")
output_date_time = DateSerial(Year(date_time), Month(date_time) + 2, 1)
MsgBox output_date_time
' 7日後
date_time = Now
output_date_time = Format(DateAdd("d", 7, date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 15日後
date_time = Now
output_date_time = Format(DateAdd("d", 15, date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 30日前
date_time = Now
output_date_time = Format(DateAdd("d", -30, date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 6ヶ月後
date_time = Now
output_date_time = Format(DateAdd("m", 6, date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 6ヶ月前
date_time = Now
output_date_time = Format(DateAdd("m", -6, date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 3週間後
date_time = Now
output_date_time = Format(DateAdd("ww", 3, date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 今週の日曜日
date_time = Now
output_date_time = Format(DateAdd("d", 1 - Weekday(date_time), date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 今週の月曜日
date_time = Now
output_date_time = Format(DateAdd("d", 2 - Weekday(date_time), date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 今週の金曜日
date_time = Now
output_date_time = Format(DateAdd("d", 6 - Weekday(date_time), date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 今週の土曜日
date_time = Now
output_date_time = Format(DateAdd("d", 7 - Weekday(date_time), date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 前週の月曜日
date_time = Now
output_date_time = Format(DateAdd("d", 2 - Weekday(date_time) - 7, date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 来週の月曜日
date_time = Now
output_date_time = Format(DateAdd("d", 2 - Weekday(date_time) + 7, date_time), "yyyy/mm/dd")
MsgBox output_date_time
' 10分前
date_time = Now
output_date_time = TimeValue(date_time) - TimeValue("00:10:00")
MsgBox output_date_time
' 10分後
date_time = Now
output_date_time = TimeValue(date_time) + TimeValue("00:10:00")
MsgBox output_date_time
' 1 年のうちで何週目かを表す数値
date_time = Now
MsgBox Format(date_time, "ww 週目")
' 英語の月名
date_time = Now
MsgBox Format(date_time, "mmmm")
' 和風月名
Dim strWafu() As Variant
strWafu = Array("睦月", "如月", "弥生", "卯月", "皐月", "水無月", "文月", "葉月", "長月", "神無月", "霜月", "師走")
date_time = Now
MsgBox strWafu(Month(date_time) - 1)
' 干支
Dim strEto() As Variant
strEto = Array("申", "酉", "戌", "亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未")
date_time = Now
MsgBox strEto(Year(date_time) Mod 12) ' 年を12で割った余りから算出
End Sub


コメント