.net 如何查找两个日期/DateTimePickers 之间的天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19017466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the number of days between two dates / DateTimePickers
提问by Ryan Harvell
In my code below, how do I set up arrivaldate and depaturedate so I can get an amount due? I need to set them up as days stayed at the hotel so I can come up with a total. If this makes any sense? I am using datetimepickers in Visual Basic.
在我下面的代码中,我如何设置到达日期和出发日期,以便我可以获得应付金额?我需要将它们设置为在酒店入住的天数,以便我可以得出总数。如果这有意义吗?我在 Visual Basic 中使用日期时间选择器。
Public Class RentalForm
'declare constants
Const tax_rate_decimal As Decimal = 12.25D
Const king_price_decimal As Decimal = 110.9D
Const queen_price_decimal As Decimal = 105.9D
Const double_price_decimal As Decimal = 95.9D
'declare variables
Private roomchargesumdecimal, taxamountsumdecimal, amountduesumdecimal As Decimal
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Close()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxnameofguest.TextChanged
End Sub
Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
'dimension local variables
Dim numberofguestsinteger As Integer
Dim roomchargedecimal, taxamountdecimal, amountduedecimal, taxratedecimal As Integer
Dim arrivaldate, departuredate As Date
Try
'dates
arrivaldate = Now
'convert quantity to numeric
numberofguestsinteger = Integer.Parse(TextBoxNumberofguests.Text)
'calculate values for single person
roomchargedecimal = numberofguestsinteger * (arrivaldate + departuredate)
taxratedecimal = roomchargedecimal * tax_rate_decimal
Catch ex As Exception
End Try
End Sub
Private Sub DateTimePickerarrivaldate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePickerarrivaldate.ValueChanged
End Sub
Private Sub Label16averagelengthofstay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label16averagelengthofstay.Click
End Sub
Private Sub RentalForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
回答by ??ssa P?ngj?rdenlarp
DateTimemath can be confusing at first. But it doesn't really matter if they are DateTimePickercontrols or variables because myDateTimePicker.Valueisa DateTimeType. So, you can mix and match variables and controls such as Arrival as Now and Departure from a picker, and just use subtraction:
DateTime数学起初可能会令人困惑。不过,这并不重要,如果他们DateTimePicker控制或变量,因为myDateTimePicker.Value是一个DateTime类型。因此,您可以混合和匹配变量和控件,例如 Arrival as Now 和 Departure from a picker,只需使用减法:
Dim arrivaldate As DateTime = DateTime.Now
Dim departuredate As DateTime = Me.DeparturePicker.Value
Dim DaysStayed as Int32 = departuredate.Subtract(arrivaldate).Days
The thing to remember is that the result is a TimeSpanobject. If you look at the structure, you'll see it provides the time elapsed in units from Daysto Ticks.
要记住的事情是结果是一个TimeSpan对象。如果您查看该结构,您会看到它提供了从Days到以单位为单位的时间Ticks。
The code above plucks the Daysvalue from the TimeSpanwithout creating a temp TimeSpanvar. Another way:
上面的代码Days从 中提取值TimeSpan而不创建临时变量TimeSpan。其它的办法:
Dim tsHotelStay = detarturedate.Value - arrivalDate
wholeDays = tsHotelStay.Days ' e.g 7
totalDays = tsHotelStay.TotalDays . e.g. 7.53
totalHrs = tsHotelStay.TotalHours . eg 180.397
This time, the code doescreate a TimeSpanvariable (tsHotelDay). Note that all the properties are available in whole and fractional forms (except Ticks).
这一次,代码确实创建了一个TimeSpan变量 ( tsHotelDay)。请注意,所有属性都有整数和小数形式(Ticks 除外)。
Finally, the 2 subtraction methods shown (DateTime.Subtract(dt)and myTs = dtA - dtB) are functionally identical: both return a TimeSpan object.
最后,所示的 2 个减法方法 (DateTime.Subtract(dt)和myTs = dtA - dtB) 在功能上是相同的:都返回一个 TimeSpan 对象。
回答by Justin R.
The DateTimePicker objects have a Value property, which will give you a DateTime for each. You can get the difference two DateTime objects by subtracting one from the other.
DateTimePicker 对象有一个Value 属性,它会为每个对象提供一个 DateTime。您可以通过从另一个中减去一个来获得两个 DateTime 对象的差异。
P.S. Don't eat all exceptions like that. It's uncool.
PS不要像那样吃所有的例外。很不爽
回答by Gruja82
Maybe you could use
也许你可以用
`DateDiff(DateInterval.Day, DateTimePicker1.Value, DateTimePicker2.Value)
to calculate number of date intervals (days) betveen two dates.
计算两个日期之间的日期间隔(天)数。

