「VB.NET」AddYearsで日時を加算する

2021年9月16日

書式
Public Shared Function AddYears (dateValue As Nullable(Of DateTime), addValue As Nullable(Of Integer)) As Nullable(Of DateTime)
Public Shared Function AddMonths (dateValue As Nullable(Of DateTime), addValue As Nullable(Of Integer)) As Nullable(Of DateTime)

使用例

Module Module1

    Sub Main()

        Dim dtm As New DateTime(2021, 9, 15, 21, 20, 10)

        '2年後 年の加算
        Dim resA As DateTime = dtm.AddYears(2)
        Console.WriteLine(resA)

        '2月後 月の加算
        Dim resB As DateTime = dtm.AddMonths(2)
        Console.WriteLine(resB)

        '2日後 日の加算
        Dim resC As DateTime = dtm.AddDays(2)
        Console.WriteLine(resC)

        '2時間後 時間の加算
        Dim resD As DateTime = dtm.AddHours(2)
        Console.WriteLine(resD)

        '2分後 分の加算
        Dim resE As DateTime = dtm.AddMinutes(2)
        Console.WriteLine(resE)


        Console.ReadKey()

    End Sub

End Module

結果
2023/09/15 21:20:10
2021/11/15 21:20:10
2021/09/17 21:20:10
2021/09/15 23:20:10
2021/09/15 21:22:10

VB.net

Posted by arkgame