473,800 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2833
Graham wrote on 10 jul 2003 in comp.lang.javas cript:
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.leng th > 2) { // seconds in hours
seconds += 60 * 60 * parseInt(timePa rts[index], 10);
index++;
}
if (timeParts.leng th > 1) { // seconds in minutes
seconds += 60 * parseInt(timePa rts[index], 10);
index++;
}
seconds += parseInt(timePa rts[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:s s" 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*****@agrico reunited.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
12346
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 minutes 5 seconds". Any ideas? Thanks very much! function calcElapsedTime($time) { // calculate elapsed time (in seconds!) $diff = time()-$time;
3
2184
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 it to get the number of seconds I am not getting an accurate number. I should only have whole numbers of seconds with nothing past the decimal point. I would like to be able to subtract the two date/times and get the number of seconds without...
2
2412
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 possible to do this? Please let me know how to do it. Thanks.
3
12831
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 for the irregular rotation of the earth, which is slowing down a little bit. So, a minute can contains 61 seconds ( range 0..60 ). Besides, the NTP protocol takes leap second into account, and UTC time also.
1
12291
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 number of seconds elapsed from the given date time to current date time.
2
1944
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 using code like this: CInt(CDate(drTime(2)).Hour / 3600) + CInt(CDate(drTime(2)).Minute * 60) + CInt(CDate(drTime(2)).Second) Is it the only way to get the total number of seconds? I tried the
5
7131
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 all of the times in hh:nn:ss format - because that is the way we thought about things around here, and which would make sense in our line of work. Well, now I am being asked to run some statistics, and my boss wants total time for created...
6
7016
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, open-source, function out there that takes a string in the form of "hh:mm:ss" (where hh is 00-23, mm is 00-59, and ss is 00-59) and converts it to an integer (ss + 60 * (mm + 60 * hh))? I'd like something that throws an exception if hh, mm, or ss...
3
3341
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 that phone call in a MS SQL Server 2000 database (which I don't own). The data is being returned to me in a view (see DDL for w_HR_Call_Log below). I link to this view in MS access through ODBC to create a linked table. I have my own view in...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.