Dim theday As New DateTime(2007, 12, 31)
Console.WriteLine(theday) ' 出力:2007/12/31 0:00:00
For n As Integer = 1 To 3
Dim newday As DateTime = theday.AddDays(n)
Console.WriteLine("{0}日後 {1}", n, newday)
Next
' 出力:
' 1日後 2008/01/01 0:00:00
' 2日後 2008/01/02 0:00:00
' 3日後 2008/01/03 0:00:00
For n As Integer = 1 To 3
Dim newday As DateTime = theday.AddDays(-n)
Console.WriteLine("{0}日前 {1}", n, newday)
Next
' 出力:
' 1日前 2007/12/30 0:00:00
' 2日前 2007/12/29 0:00:00
' 3日前 2007/12/28 0:00:00
For n As Integer = 1 To 3
Dim newday As DateTime = theday.AddMonths(n)
Console.WriteLine("{0}カ月後 {1}", n, newday)
Next
' 出力:
' 1カ月後 2008/01/31 0:00:00
' 2カ月後 2008/02/29 0:00:00
' 3カ月後 2008/03/31 0:00:00
For n As Integer = 1 To 3
Dim newday As DateTime = theday.AddMonths(-n)
Console.WriteLine("{0}カ月前 {1}", n, newday)
Next
' 出力:
' 1カ月前 2007/11/30 0:00:00
' 2カ月前 2007/10/31 0:00:00
' 3カ月前 2007/09/30 0:00:00
For n As Integer = 1 To 3
Dim newday As DateTime = theday.AddYears(n)
Console.WriteLine("{0}年後 {1}", n, newday)
Next
' 出力:
' 1年後 2008/12/31 0:00:00
' 2年後 2009/12/31 0:00:00
' 3年後 2010/12/31 0:00:00
For n As Integer = 1 To 3
Dim newday As DateTime = theday.AddYears(-n)
Console.WriteLine("{0}年前 {1}", n, newday)
Next
' 出力:
' 1年前 2006/12/31 0:00:00
' 2年前 2005/12/31 0:00:00
' 3年前 2004/12/31 0:00:00
End Sub
End Class