我遇到的问题是在VB中做数学时间。
我目前正在一所学校做志愿者,并帮助他们的前/后护理系统
因此,护理之前的目标是采用时间戳并将其与当天开始的时间进行比较,并找出在给定时间差内的半小时到达最近的半小时。
例如。一个孩子在7点36分到达早晨护理学校开始于9:30。
我希望我的程序能够做什么
9:30-7:36 = 1小时54分钟
1小时54分钟/ 30分钟= 3.XXX
将每小时的价格乘以4乘以我的成本为12
我的代码似乎已经关闭了15分钟。我将显示下面使用的代码。如果存在逻辑问题或者为时间数学选择的方法是否差,请告诉我
'Creating all my variables including half constant half variable times to
compare against
Dim DateIn As DateTime = System.DateTime.Now
Dim Month As Integer = Microsoft.VisualBasic.DateAndTime.Month(DateIn)
Dim Year As Integer = Microsoft.VisualBasic.DateAndTime.Year(DateIn)
Dim Day As Integer = Microsoft.VisualBasic.DateAndTime.Day(DateIn)
Dim DateMorning As New Date(Year, Month, Day, 7, 30, 0)
Dim DateSchoolStart As New Date(Year, Month, Day, 9, 30, 0)
Dim halfHour As System.TimeSpan = New TimeSpan(0, 0, 30, 0)
Dim ticksHalfHour As Long = halfHour.Ticks
Dim count As Integer
Dim cost As Integer
'Portion of a function that calculates cost if the time is in the right zone
If (DateIn >= DateMorning And DateIn < DateSchoolStart) Then
CareTime = DateSchoolStart.Subtract(DateIn)
Dim CareTicks As Long = CareTime.Ticks
count = ReturnCount(CareTicks, ticksHalfHour)
cost = count * price
WriteReport(cost, Year, Month, Day, name)
MessageBox.Show("Welcome to morning care " & name & "!")
Return cost
End If
' function that divides the two longs and does a mod calculation
Function ReturnCount(ByVal tick1 As Long, tick2 As Long) As Integer
If (tick1 Mod tick2 = 0) Then
count = tick1 / tick2
Return count
Else
count = 1 + (tick1 / tick2)
Return count
End If
End Function
我相信这是代码的相关部分。如果您发现我的逻辑存在缺陷或者有更简单的方法来实现我的目标,那么您的意见将非常受欢迎。