验证vb.net中两个日期之间的天数

时间:2013-09-18 09:14:28

标签: vb.net

我有一个使用2个datetimepickers的sub并在文本框中显示天数的差异,我希望sub告诉用户天数是0还是10天允许的天数在1之间然后返回用户再次选择日期

Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs)Handles btnCalculate.Click

 Dim dt1 As DateTime = Convert.ToDateTime(DateTimePicker1.Text)

            Dim dt2 As DateTime = Convert.ToDateTime(DateTimePicker2.Text)

            Dim ts As TimeSpan = dt2.Subtract(dt1)

            If Convert.ToInt32(ts.Days) >= 0 Then

                txtDays.Text = "Total Days are "

                txtDays.AppendText(Environment.NewLine & Convert.ToInt32(ts.Days))


            Else

                MessageBox.Show("Invalid Input for dates (Start Date Cannot exist before End date)")

            End If


        End If
End Sub

1 个答案:

答案 0 :(得分:1)

If ts.Days >= 1 AndAlso ts.Days <= 10 Then
    ' OK  
    txtDays.AppendText(Environment.NewLine & "Total Days are: " & ts.Days)
Else
     ' out of range
     MessageBox.Show("Days allowed are between 1 to 10.")
End If