473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Total # in seconds for a given time

I have a variable which can be in the format "mm:ss" or "hh:mm:ss". I would
like to extract the total number of seconds from this.

Ie "01:30" would be 90 seconds.

Thanks in advance !

Jul 20 '05 #1
2 2807
Graham wrote on 10 jul 2003 in comp.lang.javascript:
I have a variable which can be in the format "mm:ss" or "hh:mm:ss". I
would like to extract the total number of seconds from this.

Ie "01:30" would be 90 seconds.


<script>
function hms2sec(v){
a=v.split(":")
if (a.length==2)
r=60*a[0]+1*a[1]
else
r=60*60*a[0]+60*a[1]+1*a[2]
return r
}

alert(hms2sec("01:30"))
alert(hms2sec("12:01:30"))
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #2
Graham wrote:
I have a variable which can be in the format "mm:ss" or "hh:mm:ss". I would
like to extract the total number of seconds from this.

Ie "01:30" would be 90 seconds.

Thanks in advance !


<script type="text/javascript">
var seconds = 0, index = 0;
var t = "01:01:30";
var timeParts = t.split(/:/);
if (timeParts.length > 2) { // seconds in hours
seconds += 60 * 60 * parseInt(timeParts[index], 10);
index++;
}
if (timeParts.length > 1) { // seconds in minutes
seconds += 60 * parseInt(timeParts[index], 10);
index++;
}
seconds += parseInt(timeParts[index], 10); // seconds
alert(seconds);
</script>

This assumes your variable is either "hh:mm:ss" or "mm:ss" (it even supports
"ss"). If it's ever "dd:hh:mm:ss" or some other format, it will provide
incorrect results.

If you don't need support for "ss", you can remove the condition identified by
"// seconds in minutes".

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: NotGiven | last post by:
Below is a good elapsed time function I found. However, I'd like to return total seconds instead of broken down into days, hours, minutes & seconds. In other words, I want "125" instead of "2...
3
by: Tim | last post by:
I need to get the number of seconds when subtracting two date/times. With the code I am using below I subtract the two date/times and end up with a total of days like "1.56". Then when I multiply...
2
by: msnews | last post by:
Hi All, We have a requirement to display total number of current users using the site. We want to display only the total no other information. And this value needs to change dynamically. Is it...
3
by: nriesch | last post by:
In the documentation, the "Second" property of class DateTime is a value between 0 and 59. In UTC time, approximately every year of so, a leap second is added at 00:00:00 UTC, so as to account...
1
by: Prabhu | last post by:
Hi, Can any one help me getting Seconds elapsed from a given date time to current time in .Net?. for e.g. Suppose assume a given time date time "01-Jan-2000 00:00:00", I should get the...
2
by: Chris Leffer | last post by:
Hi. My application reads a table that stores data as time, eg: 02:17:31 I need to read this value and convert it to an integer, representing the total number of seconds. At this moment I am...
5
by: simondsm | last post by:
I have a video tape library that we use at work here in a MS-Access database. When originally desgined, we never thought ot convert the time on the fly into total seconds, but instead, we stored...
6
by: google0 | last post by:
I know this is a trivial function, and I've now spent more time searching for a surely-already-reinvented wheel than it would take to reinvent it again, but just in case... is there a published,...
3
by: Beowulf | last post by:
I have data coming from a telephony system that keeps track of when an employee makes a phone call to conduct a survey and which project number is being billed for the time the employee spends on...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.