Visual Basic サンプル集 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Visual Basic 中学校 > Visual Basic サンプル集 > Visual Basic サンプル集目次 >
日付のたし算
2020/6/16
40日後を求める
Dim sourceDate
As Date =
#8/20/2024# Dim result As Date = sourceDate.AddDays(40) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) |
実行すると 2024/09/29 と表示されます。→ Debug.WriteLineが表示される場所
60日と8時間50分23秒後 を求める
Dim sourceDate
As Date =
#8/20/2024# Dim span As New TimeSpan(60, 8, 50, 23) '60日と8時間50分23秒を表す期間 Dim result As Date = sourceDate + span Debug.WriteLine(result.ToString("yyyy\/MM\/dd HH\:mm\:ss")) |
実行すると 2024/10/19 08:50:23 と表示されます。→ Debug.WriteLineが表示される場所
40ヵ月後を求める
Dim sourceDate
As Date =
#8/20/2024# Dim result As Date = sourceDate.AddMonths(40) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) |
実行すると 2027/12/20 と表示されます。→ Debug.WriteLineが表示される場所
次の例は 2024/1/31 の1ヶ月後の日付を求めます。
2月には31日がないので結果は29日になる点がポイントです。
Dim sourceDate
As Date =
#1/31/2024# Dim result As Date = sourceDate.AddMonths(1) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) |
実行すると 2024/02/29 と表示されます。→ Debug.WriteLineが表示される場所
40年後を求める
Dim sourceDate
As Date =
#8/20/2024# Dim result As Date = sourceDate.AddYears(40) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) |
実行すると 2064/08/20 と表示されます。→ Debug.WriteLineが表示される場所
次の例では 2024/2/29 の1年後の日付を求めます。
2025年の2月には29日がないので結果は28日になる点がポイントです。
Dim sourceDate
As Date =
#2/29/2024# Dim result As Date = sourceDate.AddYears(1) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) |
実行すると 2025/02/28 と表示されます。→ Debug.WriteLineが表示される場所
40時間後を求める
Dim sourceDate
As Date =
#8/20/2024# Dim result As Date = sourceDate.AddHours(40) Debug.WriteLine(result.ToString("yyyy\/MM\/dd HH\:mm\:ss")) |
実行すると 2024/08/21 16:00:00 と表示されます。→ Debug.WriteLineが表示される場所
VB6 では DateAdd を使用します。
→
VB6
日付の足し算