Solved

# Calculate number of seconds in a given time

Posted on 2014-03-14
Medium Priority
239 Views
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
Question by:dwe0608
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 26

Expert Comment

ID: 39929105
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

LVL 26

Assisted Solution

Shaun Kline earned 750 total points
ID: 39929111
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

LVL 33

Accepted Solution

Big Monty earned 750 total points
ID: 39929202
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

LVL 1

Author Closing Comment

ID: 39930374
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month11 days, 14 hours left to enroll