Hi,
I've defined a custom function called Downtime as coded below which I then use to calculate elapsed time between 2 sets of dates and times. This works fine except when the dates span midnight and then I get really big numbers for downtime. For eg if Stop Date is 12/15/2002 and Stop Time is 23:00 and the Start Date is 12/16/2002 and Start Time is 01:00, instead of 120 minutes I get 525720. Here Stop date and Time refer to the time when the machine stops and Start date. I would appreciate any help with this as I've struggled with this problem for real long.
Public Function Downtime(ByVal Start_Time As Date, ByVal Stop_Time As Date, ByVal Start_Date As Date, ByVal Stop_Date As Date) As Double
Dim StartDate As String
Dim EndDate As String
Dim dStartDate As Date
Dim dEndDate As Date
StartDate = Format(Stop_Date, "mmm/dd/yyy" & " " & Format(Stop_Time, "Short Time"
dStartDate = CDate(StartDate)
EndDate = Format(Start_Date, "mmm/dd/yyy" & " " & Format(Start_Time, "Short Time"
dEndDate = CDate(EndDate)
Downtime = DateDiff("n", dStartDate, dEndDate)
End Function
I've defined a custom function called Downtime as coded below which I then use to calculate elapsed time between 2 sets of dates and times. This works fine except when the dates span midnight and then I get really big numbers for downtime. For eg if Stop Date is 12/15/2002 and Stop Time is 23:00 and the Start Date is 12/16/2002 and Start Time is 01:00, instead of 120 minutes I get 525720. Here Stop date and Time refer to the time when the machine stops and Start date. I would appreciate any help with this as I've struggled with this problem for real long.
Public Function Downtime(ByVal Start_Time As Date, ByVal Stop_Time As Date, ByVal Start_Date As Date, ByVal Stop_Date As Date) As Double
Dim StartDate As String
Dim EndDate As String
Dim dStartDate As Date
Dim dEndDate As Date
StartDate = Format(Stop_Date, "mmm/dd/yyy" & " " & Format(Stop_Time, "Short Time"
dStartDate = CDate(StartDate)
EndDate = Format(Start_Date, "mmm/dd/yyy" & " " & Format(Start_Time, "Short Time"
dEndDate = CDate(EndDate)
Downtime = DateDiff("n", dStartDate, dEndDate)
End Function