Visual Basic サンプル集 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Visual Basic 中学校 > Visual Basic サンプル集 > Visual Basic サンプル集目次 >
日付のひき算
2020/6/16
何日経過したかを求める
Dim date1
As Date =
#1/12/2025# Dim date2 As Date = #12/25/2024# Dim result As Integer = (date1 - date2).Days Debug.WriteLine(result) ' 18 と表示されます。 |
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/06/20 15:09:37 と表示されます。 |
→ Debug.WriteLineが表示される場所
14日前の日付を求める
Dim sourceDate
As Date =
#1/12/2025# Dim result As Date = sourceDate.AddDays(-14) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) ' 2024/12/29 と表示されます。 |
→ Debug.WriteLineが表示される場所
14ヶ月前の日付を求める
Dim sourceDate
As Date =
#1/12/2025# Dim result As Date = sourceDate.AddMonths(-14) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) ' 2023/11/12 と表示されます。 |
→ Debug.WriteLineが表示される場所
次の例は 2025/3/31 の1ヶ月前の日付を求めます。
2月には31日がないので結果は28日になる点がポイントです。
Dim sourceDate
As Date =
#3/31/2025# Dim result As Date = sourceDate.AddMonths(-1) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) ' 2025/02/28 と表示されます。 |
→ Debug.WriteLineが表示される場所
14年前の日付を求める
Dim sourceDate
As Date =
#1/12/2025# Dim result As Date = sourceDate.AddYears(-14) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) ' 2011/11/12 と表示されます。 |
メモ:紀元前は表現できません。ArgumentOutOfRangeException が発生します。
次の例は 2024/2/29 の1年前の日付を求めます。
2023年の2月には29日がないので結果は28日になる点がポイントです。
Dim sourceDate
As Date =
#2/29/2024# Dim result As Date = sourceDate.AddYears(-1) Debug.WriteLine(result.ToString("yyyy\/MM\/dd")) ' 2023/02/28 と表示されます。 |
→ Debug.WriteLineが表示される場所
何ヶ月経過したかを求める
Dim date1
As Date =
#1/12/2025# Dim date2 As Date = #6/27/2016# Dim month As Integer = 0 Do While (date2.AddMonths(month + 1) < date1) month += 1 Loop Debug.WriteLine(month) ' 102 と表示されます。 |
次の例は、1日(月の始めの日)を何回またいだかを数えます。
Dim date1
As Date =
#1/12/2025# Dim date2 As Date = #6/27/2016# Dim result As Integer = DateDiff(DateInterval.Month, date2, date1) Debug.WriteLine(result) ' 103 と表示されます。 |
何年経過したかを求める
Dim date1
As Date =
#1/12/2025# Dim date2 As Date = #6/27/2016# Dim year As Integer = 0 Do While (date2.AddYears(year + 1) < date1) year += 1 Loop Debug.WriteLine(year) ' 8 と表示されます。 |
次の例は、1月1日を何回またいだかを数えます。
Dim date1
As Date =
#1/12/2025# Dim date2 As Date = #6/27/2016# Dim result As Integer = DateDiff(DateInterval.Year, date2, date1) Debug.WriteLine(result) ' 9 と表示されます。 |
聖徳太子が生まれてから何日経ったかを求める
'この聖徳太子の生年月日は日本書紀の暦を機械的に西暦に置き換えたものであり、厳密ではありません。 Dim taishiBirthday As Date = #2/7/574# Dim totalDays As Double = (Now - taishiBirthday).TotalDays Debug.WriteLine("聖徳太子が生まれてから " & totalDays.ToString("0") & " 日経過しています。") |
弥勒が現れるまで後何年かを求める
Dim shakaDeath
As Long = -486
'釈迦が死亡したおおよその年 Dim mirokuSpan As Long = 5670000000 '釈迦死亡後、弥勒菩薩が現れるまで56億7千万年 Dim mirokuYear As Long = shakaDeath + mirokuSpan Dim untilMiroku As Long = mirokuYear - Now.Year Debug.WriteLine("弥勒が現れるまで後 " & untilMiroku & "年です。") |
メモ:太陽系ができたのが約45億年前です。それより長い時間待つ必要があります。