429,101 Members | 1,340 Online
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

# probleum in calculating the time difference

 100+ P: 198 Hi i use Datediff() and it return only hours but i want total time with minutes and seconds at the same time i use following code for all this and it works fine but when the the time came which i have mentioned in the coding produces wrong output because the difference between is 1:13:52 but it is showing 12:33:52 how can i correct this please help Dim Start As Date Dim Finish As Date Dim Duration As Date Dim outStr As String Dim Length As Integer 'Converting time values to dates Start = CDate("5:53:05 PM") Finish = CDate("6:26:57 PM") 'Find duration Duration = Finish - Start outStr = CStr(Duration) 'Convert to string an trim AM/PM indicator Length = InStr(outStr, "AM") outStr = Mid(CStr(Duration), 1, Length - 2) MsgBox (outStr) Apr 28 '08 #1
6 Replies

 Expert 5K+ P: 8,127 How the difference is 1:13:52 ? I think it is only 00:33:52. Apr 28 '08 #2

 100+ P: 198 Hi sorry but it is showing 12:33:52 and i want to show only 00:33:52 please give some idea thanks in advance How the difference is 1:13:52 ? I think it is only 00:33:52. Apr 28 '08 #3

 Expert 5K+ P: 8,127 i don't understand the following lines of your code Length = InStr(outStr, "AM") outStr = Mid(CStr(Duration), 1, Length - 2) Apr 28 '08 #4

 Expert 100+ P: 1,295 i don't understand the following lines of your code Length = InStr(outStr, "AM") outStr = Mid(CStr(Duration), 1, Length - 2) Those lines will remove characters from Duration, starting from 1 position before an 'AM' appears for the first time. i.e. If Duration = "1:22:44 AM" then outStr = "1:22:44" But if the string (Duration) does not have the string 'AM' in it,, then Length (which by the way is an awful name for a variable) will be zero, so the MID function will show an error. I dont understand them neither, because the date format might change and Duration wont have 'AM' all times... ..so, to the OP, just have in mind the Duration shouldn't be over 12 hours, so you wont have 'AM' in the string. And make sure the format is always a desirable one. HTH Apr 28 '08 #5

 100+ P: 198 Hi thanks for reply my duration is always eight hours but if you have any sugestion regarding the date format please give but my probleum is when the duration is less than one hour , then minutes and seconds are fine but hour should be in 00 format but it is displaying 12 insted of 00 e.g if duration is like 00:23:52 but with this code it is like 12:23:52 just give me suggestion how i will solve this probleum i correct my code dim Start as date dim Finish as date dim Duration as date dim startdate,enddate as string Start = CDate(StartDate) Finish = CDate(EndDate) Duration = Finish - Start msgbox Duration Those lines will remove characters from Duration, starting from 1 position before an 'AM' appears for the first time. i.e. If Duration = "1:22:44 AM" then outStr = "1:22:44" But if the string (Duration) does not have the string 'AM' in it,, then Length (which by the way is an awful name for a variable) will be zero, so the MID function will show an error. I dont understand them neither, because the date format might change and Duration wont have 'AM' all times... ..so, to the OP, just have in mind the Duration shouldn't be over 12 hours, so you wont have 'AM' in the string. And make sure the format is always a desirable one. HTH Apr 29 '08 #6

 100+ P: 180 hi use following to show duration MsgBox Format(Duration, "hh:mm:ss") regards manpreet singh dhillon hoshiarpur Apr 30 '08 #7