根据2个文本框VB.NET中的时间差计算付款金额



在此处输入图像描述

我想根据时间和超时的时间差来获得付款的金额。例子。时间:7:00:00 超时是:13:00:00 区别是6小时可以说,每小时汇率为10.00,因此金额应为60.00谢谢!我正在使用vb.net

我想做的就是这样。

Private Const Format As String = "HH:mm:ss"
'this is the code i used to get the time in
Private Sub btnTimeIn_Click(sender As Object, e As EventArgs) Handles btnTimeIn.Click
     TextboxTimeIn.Text = DateTime.Now.ToString(Format)
End Sub
'this is the time out
Private Sub btnTimeOut_Click(sender As Object, e As EventArgs) Handles btnTimeOut.Click
    TextboxTimeOut.Text = DateTime.Now.ToString(Format)
    'this is what is use to get the time difference
    txtAmount.Text = Convert.ToDateTime(TextboxTimeOut.Text).Subtract(Convert.ToDateTime(TextboxTimeIn.Text)).ToString()     
End Sub

,但是我不想显示时间差,而是想显示txtamount中的数量。示例

如果定时差< = 60分钟,则 txtamount = 10

其他时间差> 60分钟,然后txtamount = 20

您想知道3件事:

  1. 检查和结帐之间的时间
  2. 那几个小时
  3. HOM会花很多钱

这些要点只能以一行计算,但是为了清楚起见,我们将一一清除它们:

'let's use some more variables to make this easier
Private Const Format As String = "HH:mm:ss"
Private checkin As DateTime
Private checkout As DateTime
Private rate As Integer = 10
'now with variables!
Private Sub btnTimeIn_Click(sender As Object, e As EventArgs) Handles btnTimeIn.Click
    checkin = Now
    TextboxTimeIn.Text = checkin.ToString(Format)
End Sub
Private Sub btnTimeOut_Click(sender As Object, e As EventArgs) Handles btnTimeOut.Click
    checkout = Now
    TextboxTimeOut.Text = checkout.ToString(Format)
    'First we check for an amount of hours, then we add one if they overstayed
    Dim timeStayed As Integer = checkout.Hour - checkin.Hour
    If TimeStayed.Minute > 0 Then timeStayed += 1
    'Notice the use of a variable so you can tweak your rates easily
    txtAmount.Text = (timeStayed * rate).ToString
End Sub

您需要记住的是:

  1. 使您更容易。不要一直转换一切。
  2. 遵循自己的伪代码。您已经知道该怎么办。做。
  3. 我用整数赚钱...这很糟糕!您应该尽快更改此功能,以便您的数字有小数!(您也应该将txtamount格式化为金钱)
  4. 玩得开心!

最新更新