根据2个文本框中的时差计算要支付的金额VB.Net

时间:2019-03-02 11:43:23

标签: vb.net

enter image description here

我想根据输入和输出的时间差来获取支付的金额。 例。时间是: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中显示金额。例子

如果timeDifference <= 60mins,则   txtAmount = 10

else timeDifference> 60分钟,然后 txtAmount = 20

1 个答案:

答案 0 :(得分:0)

您想了解三件事:

  1. 办理入住和退房之间经过了多少时间
  2. 那是几个小时
  3. 这将花费多少钱

这些点只能在一行中计算,但为清楚起见,我们将一一清除它们:

'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. 我用Integers赚钱...这很糟糕!您应该尽快更改此设置,以便您的数字可以为小数! (您还应该将txtAmount设置为货币格式)
  4. 玩得开心!