.net 在Visual Basic中遇到时间跨度问题

.net 在Visual Basic中遇到时间跨度问题,.net,vb.net,timespan,.net,Vb.net,Timespan,在VB.net中,我一直在学习Murach的Visual Basic 2010。在学习如何处理时间和字符串时,我遇到了时间跨度的问题 我需要采取2个日期,并找到他们之间的天数 所以我声明了我的变量 Dim currentDay As Date Dim futureDate As Date Dim timespan As TimeSpan = currentDay.Subtract(futureDate) Dim strMsgText As String Dim daysUntilDue = ti

在VB.net中,我一直在学习Murach的Visual Basic 2010。在学习如何处理时间和字符串时,我遇到了时间跨度的问题

我需要采取2个日期,并找到他们之间的天数

所以我声明了我的变量

Dim currentDay As Date
Dim futureDate As Date
Dim timespan As TimeSpan = currentDay.Subtract(futureDate)
Dim strMsgText As String
Dim daysUntilDue = timespan.Days
然后我为currentDay和futureDate设置格式

currentDay = Convert.ToDateTime(Now)
futureDate = Convert.ToDateTime(txtFutureDate.Text) 'input from user
然后我设置需要显示的数据

 strMsgText = "Current Date: " & currentDay.ToShortDateString() _
            & "Future Date: " & futureDate.ToShortDateString() _
            & "Days Util Due " & daysUntilDue
接下来,我提供了数据验证

If IsDate(txtFutureDate.Text) Then
   futureDate = CDate(txtFutureDate.Text)
End If
最后我显示数据

MessageBox.Show(strMsgText)
我没有收到语法错误或vb ide错误 但是,当它计算日期时,它会在消息框中为我提供此信息

我试图在计算中改变日期

例如,我将其设置为
futureDate.Subtract(当前日期)
,而不是
currentDay.Subtract(当前日期)
,只是想看看它是否会给我一个不同的结果。但是,唉,结果仍然是0


我知道我做了一些错误的事情,但我无法发现IDE/编译器没有给我任何错误,这本书也没有给我任何建议,也不知道如何让它正常工作。

问题是,在您设置timespan值时,
currentDay
futureDate
尚未初始化,并且具有
DateTime
的默认值。这些值的减法将始终为0
TimeSpan

设置这两个日期后,设置时间间隔

Dim currentDay As Date
Dim futureDate As Date
Dim strMsgText As String

currentDay = Convert.ToDateTime(Now)
futureDate = Convert.ToDateTime(txtFutureDate.Text) 'input from user

Dim timespan As TimeSpan = currentDay.Subtract(futureDate)
Dim daysUntilDue = timespan.Days

由于需要检查用户是否输入了有效日期,因此可以使用DateTime.TryParse确定:

Dim currentDay As Date = DateTime.Now
Dim futureDate As Date
Dim strMsgText As String
Dim daysUntilDue As Integer

currentDay = DateTime.Now

Dim ci As New Globalization.CultureInfo("en-US")

' check if a parseable date has been entered before doing the calculation
If DateTime.TryParse(txtFutureDate.Text, ci, Globalization.DateTimeStyles.AllowWhiteSpaces, futureDate) Then
    daysUntilDue = (futureDate - currentDay).Days
    strMsgText = "Current Date: " & currentDay.ToShortDateString() & " Future Date: " & futureDate.ToShortDateString() & " Days Until Due " & daysUntilDue.ToString
Else
    strMsgText = "I could not understand the future date as entered."
End If

MessageBox.Show(strMsgText)

好的,谢谢。在currentDay和futureDate被赋值后,将代码与timespan和daysUntilDue一起放入,并将我的公式反转为futureDate.Subtrace(currentDay)。卡巴姆:效果很好。我知道这一定是逻辑错误我不是世界上最聪明的蜡笔box@thewaytonever-没问题。希望你明白你错在哪里,以及为什么这是答案。@thewaytonever-我们都从某个地方开始。当谈到编程时,事情并不明显或自然。VisualBasic绝对需要按执行顺序安排一切,以便逐行运行程序。
Dim currentDay As Date = DateTime.Now
Dim futureDate As Date
Dim strMsgText As String
Dim daysUntilDue As Integer

currentDay = DateTime.Now

Dim ci As New Globalization.CultureInfo("en-US")

' check if a parseable date has been entered before doing the calculation
If DateTime.TryParse(txtFutureDate.Text, ci, Globalization.DateTimeStyles.AllowWhiteSpaces, futureDate) Then
    daysUntilDue = (futureDate - currentDay).Days
    strMsgText = "Current Date: " & currentDay.ToShortDateString() & " Future Date: " & futureDate.ToShortDateString() & " Days Until Due " & daysUntilDue.ToString
Else
    strMsgText = "I could not understand the future date as entered."
End If

MessageBox.Show(strMsgText)