一起增加时间 [英] adding time together
问题描述
我现在看不到所有树木的木头....
我正在制定时间计划,以便员工签入和签出.
在网格中,我显示了所有工作日并添加了该天的总工作小时数( GetTotalTime ).这意味着在网格初始化行期间,我使用以下代码:
I can''t see the wood for all the trees now....
I am work on a time program, for staff to check in and out.
in a grid i show all the days working and add the total hours work for that day(GetTotalTime). that means during the grid Initialize Row i use this:
Private Sub GrdOverViewInitializeRow(sender As Object, e As Infragistics.Win.UltraWinGrid.InitializeRowEventArgs) Handles grdOverView.InitializeRow
'Calculate daily hours and enter this in cell 9
StartTime = e.Row.Cells(4).Text.ToString
StartDate = e.Row.Cells(5).Text.ToString
FinishTime = e.Row.Cells(6).Text.ToString
FinishDate = e.Row.Cells(7).Text.ToString
StartTime = String.Format("{0} {1}", StartDate, StartTime)
FinishTime = String.Format("{0} {1}", FinishDate, FinishTime)
WorkTime = GetTotalTime(StartTime, FinishTime)
grdOverView.Text = CalculateTime(WorkTime)
e.Row.Cells(9).Value = WorkTime
End Sub
票价如此之好,但是在此期间,我使用 Calculatetime 来计算总计:
So fare so good, however during this i use Calculatetime as below for Grand total:
Friend Function CalculateTime(ByVal calworktime As String) As String
Try
Dim input As String = calworktime
Dim parts = input.Split(":"c)
Dim hours = Int32.Parse(parts(0))
Dim minutes = Int32.Parse(parts(1))
Dim result = New TimeSpan(hours, minutes, 0)
TotalTime = TotalTime.Add(result)
Dim s As String = String.Format("{0}:{1}", TotalTime.TotalHours.ToString("00"), TotalTime.Minutes.ToString("00"))
CalculateTime = String.Format("Total time: {0}", s.ToString)
Return CalculateTime
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
Return Nothing
End Try
End Function
这就是问题所在.如果只是将整个工作时间加在一起,就没问题.
如何进行示例采样:5小时+ 45分钟= 6小时45分钟...额外的时间是哪里来的???
WorkTime和TotalTime在常规模块中声明.
我失去情节的任何想法...
Thnaks
This is where it goes wrong. if it is just adding whole hours together, no problems.
howere for exsample: 5 hrs + 45 min=6hrs and 45min...where did the extra hour come from?????
WorkTime and TotalTime is declare in a general module.
Any ideas where i am losing the plot...
Thnaks
推荐答案
进行任何形式的算术运算的规则之一:不要重新发明轮子.
.NET框架包括两个专门用于处理日期和时间的类:DateTime和TimeSpan.不要将字符串用于算术,而应结合使用这两个类.它们使生活变得更加轻松...
Rule number one of doing any form of arithmetic: do not re-invent the wheel.
The .NET framework includes two classes specifically for dealing with dates and times: DateTime and TimeSpan. Instead of using strings for arithmetic, use a combination of these two classes. They make life so much easier...
这似乎在起作用:
之所以返回字符串,是因为它在工资单的底部使用了
这意味着它必须显示"HH:mm"格式
但是任何人都有更快或更更好的方法,请让我知道.
朋友函数CalculateTime(ByVal calworktime为字符串)为String
试试
昏暗的输入为字符串= calworktime
昏暗的部分= input.Split(:" c)
昏暗小时= Int32.Parse(parts(0))
昏暗分钟= Int32.Parse(parts(1))
昏暗的结果=新的TimeSpan(小时,分钟,0)
TotalTime = TotalTime.Add(结果)
CalculateTime = String.Format("Ialt timer brugt:{0} {1}",Format((TotalTime.Days * 24)+ TotalTime.Hours,#00"),Format(TotalTime.Minutes,:#00" ))
返回CalculateTime
异常捕获
MessageBox.Show(ex.Message.ToString)
一无所有
结束尝试
结束功能
This seems to be working:
Reason this is a string return, is that is it used at the bottom of the payslip
which means it have to show "HH:mm" format
however anybody got a fast or more better way, please let me know.
Friend Function CalculateTime(ByVal calworktime As String) As String
Try
Dim input As String = calworktime
Dim parts = input.Split(":"c)
Dim hours = Int32.Parse(parts(0))
Dim minutes = Int32.Parse(parts(1))
Dim result = New TimeSpan(hours, minutes, 0)
TotalTime = TotalTime.Add(result)
CalculateTime = String.Format("Ialt timer brugt: {0}{1}", Format((TotalTime.Days * 24) + TotalTime.Hours, "#00"), Format(TotalTime.Minutes, ":#00"))
Return CalculateTime
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
Return Nothing
End Try
End Function
这篇关于一起增加时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!