2014-03-26 28 views
2

我对像1AM波纹管这样的日期提出了一个小问题,但如果我使用这样的日期,它工作正常“MAR 28,2014 3PM”由于某些原因,我得到一个“-17: 49“,而使用1AM ..这是为什么?问题与计数器和日期

Dim myTime As String = "MAR 26, 2014 1AM" 
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy") 
Dim date2 As DateTime = Convert.ToDateTime(myTime) 
Dim ts As New TimeSpan 
ts = date2 - date1 

If ts.Days > 0 Then 
    TextBox.Text = ts.Days & ":" & ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds 
ElseIf ts.Hours > 0 Then 
    TextBox.Text = ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds 
Else 
    TextBox.Text = ts.Minutes & ":" & ts.Seconds 

    If ts.Minutes = 2 And ts.Seconds < 30 Then 
     doStop = True 'we are 2:30 minutes away from closing 
    End If 

    If ts.Minutes < 15 Then 
       'TextBox.text = "Ending Soon" 
    End If 
End If 
+0

将选项Strict On添加到代码的顶部*或*将其设置在项目的属性中。然后修复显示的错误。 –

回答

3

您正在将当前日期/时间转换为字符串,然后在第二行立即自动将其转换回日期值。

Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy") 
              ^^^^^^^^ this converts date to string 
      ^^^^^^^^ this converts it back due to implicit casting 

但因为你正在使用hh作为格式,你得到的只是日期的12小时值,所以有关日期值的一些信息丢失。

例如,目前我正在测试代码的上午12:34,但如果在第二行之后立即转储出date1的内容,它已经变成12小时后了!

3/26/2014 12:34:41 PM 

当你想用日期值和做数学在他们身上,让他们为日期值。在准备向用户显示某些内容之前,不要转换为字符串。所以只需使用:

Dim date1 as DateTime = System.DateTime.Now