Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim UTCTime As Date = TextBox1.Text
Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
Dim beforeVal As New TimeSpan(168, 59, 59)
Dim beforeVal1 As New TimeSpan(72, 59, 59)
Dim beforeVal2 As New TimeSpan(23, 59, 59)
Label1.Text = IndianTime.AddSeconds(-beforeVal.TotalSeconds).ToString("G")
Label2.Text = IndianTime.AddSeconds(-beforeVal1.TotalSeconds).ToString("G")
Label3.Text = UTCTime.AddSeconds(-beforeVal2.TotalSeconds).ToString("G")
'////////////
Dim UTCTime1 As Date = Date.UtcNow
Dim IndianTime1 As DateTime = UTCTime1.AddHours(5.5)
Label4.Text = IndianTime1.ToString("G")
If CType(Label4.Text, Date) >= CType(Label3.Text, Date) Then
Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date"
ElseIf CType(Label4.Text, Date) >= CType(Label2.Text, Date) Then
Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date"
ElseIf CType(Label4.Text, Date) >= CType(Label1.Text, Date) Then
Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date"
Else
Label5.Text = "Print"
End If
End Sub
错误:
它将始终在label5中显示msg 对不起!门票不能在当天或旅行日期后取消 如果我只使用单端if语句那么它工作正常。 ...如果我使用上面提到的3个条件,它将在label5中显示错误消息为抱歉!机票不能在当天或旅行日期后取消
如果我使用它而不是上面的代码....那么它工作正常
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim UTCTime1 As Date = Date.UtcNow
Dim IndianTime1 As DateTime = UTCTime1.AddHours(5.5)
Label4.Text = IndianTime1.ToString("G")
If CType(Label4.Text, Date) >= CType(Label3.Text, Date) Then
Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date"
Else
Label5.Text = "Print"
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim UTCTime As Date = TextBox1.Text
Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
Dim beforeVal As New TimeSpan(168, 59, 59)
Dim beforeVal1 As New TimeSpan(72, 59, 59)
Dim beforeVal2 As New TimeSpan(23, 59, 59)
Label1.Text = IndianTime.AddSeconds(-beforeVal.TotalSeconds).ToString("G")
Label2.Text = IndianTime.AddSeconds(-beforeVal1.TotalSeconds).ToString("G")
Label3.Text = UTCTime.AddSeconds(-beforeVal2.TotalSeconds).ToString("G")
End Sub
答案 0 :(得分:1)
在没有运行代码的情况下,我能看到的最明显的事情是:
Dim UTCTime As Date = TextBox1.Text
您正在尝试将字符串分配给Date
变量。使用DateTime.TryParse
或DateTime.TryParseExact
将字符串安全地转换为日期。
<强>更新强>
我现在更仔细地查看了您的代码,并找到了以下内容:
DateTime
值,转换为您转换回DateTime
值的字符串以进行比较。为什么不首先使用DateTime
值?每次转换都是可能的失败点。所以我建议你这样编写你的方法:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim UTCTime As Date
If DateTime.TryParse(TextBox1.Text, UTCTime) Then
Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
Dim comparisonDateTime As DateTime = IndianTime.Add(New TimeSpan(23, 59, 59))
''#////////////
Dim utcNow As Date = Date.UtcNow
Dim IndianTime1 As DateTime = utcNow.AddHours(5.5)
If IndianTime1 >= comparisonDateTime Then
Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date"
Else
Label5.Text = "Print"
End If
Else
''# TextBox1 did not contain a valid date, inform the user in some way
End If
End Sub
答案 1 :(得分:0)
为什么要使用Label.Text属性进行比较?
标签没有有意义的名称,它们是从您已有的DateTime变量中转换而来的。
您有三个由ElseIf
链接的条件,这些条件都会导致相同的操作,这应该是一个If
OrElse
s。
正如Fredrik指出的那样,初始隐式分配将无法通过严格的转换。
简化:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim UTCTime As Date = DateTime.Parse(TextBox1.Text)
Dim IndianTime As DateTime = UTCTime.AddHours(5.5)
Dim IndianTime1 As DateTime = Date.UtcNow.AddHours(5.5)
Dim beforeValOffset As New TimeSpan(168, 59, 59)
Dim beforeVal1Offset As New TimeSpan(72, 59, 59)
Dim beforeVal2OffSet As New TimeSpan(23, 59, 59)
Dim beforeVal As DateTime = IndianTime.Subtract(beforeValOffset)
Dim beforeVal1 As DateTime = IndianTime.Subtract(beforeVal1Offset)
Dim beforeVal2 As DateTime = UTCTime.Subtract(beforeVal2OffSet)
Label1.Text = beforeVal.ToString("G")
Label2.Text = beforeVal1.ToString("G")
Label3.Text = beforeVal2.ToString("G")
Label4.Text = IndianTime1.ToString("G")
If (IndianTime1 >= beforeVal2) OrElse _
(IndianTime1 >= beforeVal1) OrElse _
(IndianTime1 >= beforeVal) Then
Label5.Text = "Sorry ! Ticket cannot be cancelled on same day or after journey date"
Else
Label5.Text = "Print"
Endif
End Sub
我稍微改变了你的功能。我仍然不知道你真正希望实现什么,但我希望重写可以帮助你了解你实际在做什么。