代表者の戯言

日付の演算に関しての忘備録



これもよくやるのだが、日付に日数を加算したり減算したりするプログラムを忘備録として紹介したい



datetime

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


'本日の日付をtx1に出力する

Dim dt As DateTime = DateTime.Now

Dim stdt As String


dt = dt.ToString

stdt = dt.ToShortDateString()

TextBox1.Text = stdt


End Sub



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click


'tx1に書かれてある日付とtx2に書かれている日付の差をtx3に出力する

Dim dt As DateTime

dt = TextBox1.Text


Dim tx2 As DateTime

tx2 = TextBox2.Text


Dim span As TimeSpan

span = tx2.Subtract(dt)


Dim tx3 As String

tx3 = span.Days

TextBox3.Text = tx3


End Sub


Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


'tx1に書かれてある日付とtx4に書かれている数字をtx5に出力する

Dim dt As DateTime

dt = TextBox1.Text


Dim tx4 As Integer

tx4 = TextBox4.Text


Dim plus As DateTime

plus = dt.AddDays(tx4)


TextBox5.Text = plus.ToShortDateString()


End Sub