如何查找两个日期之间的天数/ DateTimePickers

编程入门 行业动态 更新时间:2024-10-10 18:23:51
本文介绍了如何查找两个日期之间的天数/ DateTimePickers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我下面的代码中,我如何设置到达日期和过期日期,以便可以获得一笔金额?我需要设置他们,因为几天呆在酒店,所以我可以想出一个总计。如果这有任何意义吗?我在Visual Basic中使用datetimepickers。

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.

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

推荐答案

DateTime 数学可能会令人困惑。但是如果它们是 DateTimePicker 控件或变量并不重要,因为 myDateTimePicker.Value 是 a DateTime 类型。因此,您可以混合和匹配变量和控件,例如从选择器到达和离开,然后使用减法:

DateTime math can be confusing at first. But it doesn't really matter if they are DateTimePicker controls or variables because myDateTimePicker.Value is a DateTime Type. So, you can mix and match variables and controls such as Arrival as Now and Departure from a picker, and just use subtraction:

Dim arrivaldate As DateTime = DateTime.Now Dim departuredate As DateTime = Me.DeparturePicker.Value Dim DaysStayed as Int32 = departuredate.Subtract(arrivaldate).Days

要记住的是,结果是一个 TimeSpan 对象。如果您查看结构,您将看到它提供以 Days 至 Ticks 为单位的时间。

The thing to remember is that the result is a TimeSpan object. If you look at the structure, you'll see it provides the time elapsed in units from Days to Ticks.

上面的代码从 TimeSpan 不创建临时 TimeSpan var。另一种方式:

The code above plucks the Days value from the TimeSpan without creating a temp TimeSpan var. Another way:

Dim tsHotelStay = detarturedate.Value - arrivalDate wholeDays = tsHotelStay.Days ' e.g 7 totalDays = tsHotelStay.TotalDays . e.g. 7.53 totalHrs = tsHotelStay.TotalHours . eg 180.397

这一次,代码 创建一个 TimeSpan 变量( tsHotelDay )。请注意,所有属性都可以使用整数和分数形式(除了Ticks)。

This time, the code does create a TimeSpan variable (tsHotelDay). Note that all the properties are available in whole and fractional forms (except Ticks).

最后,显示了2个减法方法( DateTime.Subtract dt)和 myTs = dtA - dtB )在功能上是相同的:都返回一个TimeSpan对象。

Finally, the 2 subtraction methods shown (DateTime.Subtract(dt) and myTs = dtA - dtB) are functionally identical: both return a TimeSpan object.

更多推荐

如何查找两个日期之间的天数/ DateTimePickers

本文发布于:2023-11-22 07:57:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:天数   日期   两个   DateTimePickers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!