PDA

View Full Version : Convert Seconds To HH:MM:SS


Basscyst
10-31-2004, 03:54 PM
Could reorganize to make it a bit shorter, but. . .if it ain't broke. ;)


//*******************************************************
// Convert seconds to HH:MM:SS
//*******************************************************

function convertHMS(sec)
{

//From JS FAQ by Liorean
Number.prototype.toDecimals=function(n){
n=(isNaN(n))?
2:
n;
var
nT=Math.pow(10,n);
function pad(s){
s=s||'.';
return (s.length>n)?
s:
pad(s+'0');
}
return (isNaN(this))?
this:
(new String(
Math.round(this*nT)/nT
)).replace(/(\.\d*)?$/,pad);
}
if(sec>59)
{
var hrs=sec/3600;
if(hrs<0)
{
hrs="00";
var min=hrs*60;
min=min.toDecimals(8);
var snd=min.substring(min.indexOf('.'),min.length);
min=min.substring('0',min.indexOf('.'));

if(min<10)
{
min='0'+min
}
snd=Math.round(snd*60);
if(snd<10)
{
snd='0'+snd;
}
var tm=hrs+':'+min+':'+snd;
}
else
{

hrs=hrs.toDecimals(8);
var min=hrs.substring(hrs.indexOf('.'),hrs.length)

hrs=hrs.substring('0',hrs.indexOf('.'));

if(hrs<10)
{
hrs='0'+hrs;
}
min=min*60
min=min.toDecimals(8);
var snd=min.substring(min.indexOf('.'),min.length);
min=min.substring('0',min.indexOf('.'));

if(min<10)
{
min='0'+min
}
snd=Math.round(snd*60);
if(snd<10)
{
snd='0'+snd;
}
var tm=hrs+':'+min+':'+snd;
}
}
else
{
if(sec<10)
{
sec="0"+sec;
}
var tm="00:00:"+sec
}
return tm;
}


Basscyst

codegoboom
11-01-2004, 02:38 AM
This seems to work too... ;)
function cheat(secs)
{
var t = new Date(1970,0,1);
t.setSeconds(secs);
return t.toTimeString().substr(0,8);
}


...but only up to 23:59:59
more cheating seems to fix that...

function cheat2(secs)
{
var t = new Date(1970,0,1);
t.setSeconds(secs);
var s = t.toTimeString().substr(0,8);
if(secs > 86399)
s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
return s;
}
Well anyway, I like your function too. :)

ca_redwards
11-01-2004, 05:39 PM
function hms(secs){
time=[0,0,secs];
for(var i=2;i>0;i--){
time[i-1]=Math.floor(time[i]/60);
time[i]=time[i]%/**/60;
if(time[i]<10)
time[i]='0'+time[i];
};
return time.join(':')
}

I actually needed the empty comment between the mod operator and the 60. Otherwise, the browser would try to take them together as character code 60 (issuing a syntax error).

liorean
11-01-2004, 06:27 PM
var t = new Date(1970,0,1);could as well be var t = new Date(0);

Basscyst
11-02-2004, 12:09 AM
LOL - :o

Oh well, got the job done. :p

Basscyst

Psychox
12-18-2009, 03:25 PM
Can anybody tell me how modify this script so it'll start from 00:00:00 after 23:59:59?

jmrker
12-18-2009, 09:38 PM
Here's both versions of the script in a test program.

Note that 86400 seconds have been added to current time in test to simulate values past 23:59:59
All I did is to modulo the seconds value at the start of each function
against a full day of seconds 86400 = (24*60*60) to force final time to be less than 24:00:00.


<html>
<head>
<title>HMS from Seconds</title>
<script type="text/javascript">
// From: http://codingforums.com/showthread.php?t=46860

// this function not working right yet
function hms(secs){
secs = secs % 86400;
time=[0,0,secs];
for(var i=2;i>0;i--){
time[i-1]=Math.floor(time[i]/60);
time[i]=time[i]%/**/60;
if(time[i]<10)
time[i]='0'+time[i];
}
return time.join(':')
}

/* Another version */
function hms2(secs) {
secs = secs % 86400;
var t = new Date(1970,0,1);
t.setSeconds(secs);
var s = t.toTimeString().substr(0,8);
if(secs > 86399)
s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
return s;
}
/* */

function NOW() {
/* */
var now = new Date();
var hrs = parseInt(now.getHours(),10);
var min = parseInt(now.getMinutes(),10);
var sec = parseInt(now.getSeconds(),10);
var ctime = hrs*60*60 + min*60 + sec;
ctime += 86400; // add another 24 hours worth of seconds as a test

alert(hrs+' '+min+' '+sec+'\n ctime: '+ctime+'\n\t-- '+hms(ctime)+'\n\t-- '+hms2(ctime));
/* */
}
</script>
</head>
<body>
<button onclick="NOW()">Seconds from midnight</button>
</body>
</html>

Both seem to work equally well ... your choice to pick.
Good Luck!
:)

Psychox
12-21-2009, 07:19 AM
Thank You Jmrker :)