Elapsed Time Returning a Negative Number

mmieher

Active Member
Licensed User
Longtime User
Hi.

The following code below consistently returns -21.6 hours as the elapsed time between 11/28/2012 14:27:28 and 11/28/12 16:53:49.

Any ideas?

Thanks,
Marc

-------------------------------------------------------------------Dim TotalHrs As dim TotalHrs, WOHours as Double
Dim sDate, eDate, sTime, eTime As String
Dim Date1, Date2 As Long
Dim Elapsed As Long
Dim Seconds, Minutes, Hours, Days As Int

DateTime.DateFormat = "MM/dd/yyyy"
DateTime.TimeFormat = "HH:mm:ss"

sDate = "11/28/2012"
eDate = "11/28/2012"
sTime = "14:27:28"
eTime = "16:53:49"


If sDate <> "" AND eDate <> "" Then
Date1 = DateTime.DateParse(sDate) + DateTime.TimeParse(sTime)
Date2 = DateTime.DateParse(eDate) + DateTime.TimeParse(eTime)

Elapsed = Date2 - Date1

Seconds = Round(Elapsed / 1000)

WOHours = Seconds / 3600
TotalHrs = TotalHrs + WOHours

If Elapsed < 0 Then
Msgbox(Seconds,WOHours)
End If

End If
 

lagore

Active Member
Licensed User
Longtime User
Not sure what you are doing but the code works perfectly returning 8781 seconds or 2hr 26min 21sec, btw use code tags as it makes reading the code in your post much easier.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Works on mine too. The difference you seem to get is 'the real result'-24hours by the way.
 
Upvote 0

mmieher

Active Member
Licensed User
Longtime User
Thanks, Guys.

However, I am still stumped. Thinking that possibly something about the rest of my project was affecting this, I copied this code only into a new project. Same negative result.

Maybe a version issue?

Also, what is a "code tag"?

Marc
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Your code is correct as mentioned by the other two posts. It yields 8781 sec. When you are creating your post, to get the code in the proper format, highlight all the code, then click on the # sign (wrap code tag around selected text) located in the second horizontal bar menu. Then, when you click the 'Preview Post' button to the right of the 'Submit' button, you can see your code properly formatted.

B4X:
Dim sDate, eDate, sTime, eTime As String
Dim Date1, Date2 As Long
Dim Elapsed As Long
Dim Seconds, Minutes, Hours, Days As Int
Dim  WOHours, TotalHrs As Float
DateTime.DateFormat = "MM/dd/yyyy"
DateTime.TimeFormat = "HH:mm:ss"
sDate = "11/28/2012"
eDate = "11/28/2012"
sTime = "14:27:28"
eTime = "16:53:49"

If sDate <> "" AND eDate <> "" Then
   Date1 = DateTime.DateParse(sDate) + DateTime.TimeParse(sTime)
   Date2 = DateTime.DateParse(eDate) + DateTime.TimeParse(eTime)
   Elapsed = Date2 - Date1
   Seconds = Round(Elapsed / 1000)
   WOHours = Seconds / 3600 
   TotalHrs = TotalHrs + WOHours
   Msgbox(" Seconds:" & Seconds & "  WO hours: " & Round2(WOHours,2) & "  Total hrs: " & Round2(TotalHrs,2),"")
End If
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Thanks, Guys.

However, I am still stumped. Thinking that possibly something about the rest of my project was affecting this, I copied this code only into a new project. Same negative result.

Maybe a version issue?

Also, what is a "code tag"?

Marc

Can you post the whole sub?
 
Upvote 0
Top