• Status: Solved
• Priority: Medium
• Security: Public
• Views: 248

# Calculate number of seconds in a given time

Hi guys,

Using ASP/VB6

If I have a given time - say 04:00:20 AM (that's 4 hours, 0 minutes and 20 secs)

I am looking for a function which will give me the total number of seconds in the given time and also a function that does the same thing for minutes (rounding the seconds up or down using 30 second intervals)

MTIA

DWE
0
dwe0608
• 2
2 Solutions

If you split the given time on the space, then split the first element of the resulting array on the colon, you would end up with an array containing three elements. From there, you can calculate the seconds by:
((<element 1> * 60) + <element 2>) * 60 + <element 3>

For minutes, the calculation is similar:
(<element 1> * 60) + <element 2> + iif(<element 3> >= 30, 1, 0)

If you need to concern yourself with AM/PM, check if the first array's second element is "PM", and if so, add 12 to the hours component (<element 1> in the formulas).
0

Actually, even easier would be to use the DATEDIFF function. Just build a date to include as part of your time, and do a date diff between that date/time and just the date.

I.e. use '3/14/2014' as the start date, and '3/14/2014 4:00:20 AM' as your second date.
0

Senior Web Developer / CEO of ExchangeTree.org Commented:
just use the following:

timeToCheck = "1/1/2000 04:00:20 AM"
timeStart = "1/1/2000 00:00:00 AM"
numSecs = datediff( "s", timeStart, timeToCheck )
numMins = datediff( "m", timeStart, timeToCheck )
0

Author Commented:
Thanks guys

timeToCheck = "1/1/2000 04:00:20 AM"
timeStart = "1/1/2000 00:00:00 AM"
numSecs = datediff( "s", timeStart, timeToCheck )
numMins = datediff( "n", timeStart, timeToCheck )

"n" is for mins ....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.