PDA

View Full Version : turn milliseconds into time elapsed ~


firepages
02-15-2003, 05:27 AM
I know what I mean - not how to say it !

I gave managed to get the number of milliseconds between 2 dates from <select inputs via the date object,

so now I have the variable elapsedSeconds which may be say 60000 (1 minute)

I want to be able to display the elapsed time in human readable format say

"0 hours , 1 minute , 10 seconds"

from elapsedSeconds , any ideas how I would do this ? - I have looked at the date object and can do all sorts of things except the thing I want to :)

I know I can probably divide by 60 etc but was wondering if there was an easier way ?

Algorithm
02-15-2003, 05:44 AM
Nope, dividing by 60 is the easiest. Date objects handle absolutes, not intervals. :)

whammy
02-15-2003, 10:10 PM
I noticed that milliseconds don't work when you're trying to determine someone's birthday using the date methods - they were up to 5 or 6 days off depending upon what I tried, so I had to use another method.

ahosang
02-16-2003, 01:30 AM
Are you saying there's a bug in Date object methods still??? Thre used to be bugs in Date object in the very early browsers like NS 2, 3 etc, but I can't imagine what you mean. Could you give a code example whammy?

whammy
02-16-2003, 01:34 AM
Ok, this is the code I ended up with (which works, by the way), but I was trying to convert the dates to milliseconds instead (no problem) in the isOfLegalAge() function, and figure out if someone was either exactly 18 or older depending upon the birthdate they entered. and whenever I converted to milliseconds and figured it that way, it was off by around 5 days or so, depending upon how long ago it was.

So I ended up using the method you see here. I know it would be off if the client's time was off anyway, like all client-side scripts, but I enjoy writing this stuff for people that need some client-side solution.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>isOfLegalAge()</title>
<script type="text/javascript">
<!--

function isDate(mm,dd,yyyy) {
var d = new Date(mm + "/" + dd + "/" + yyyy);
return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy;
}

function isOfLegalAge(mm,dd,yyyy) {
var d = new Date(mm + "/" + dd + "/" + yyyy);
var t = new Date();
var diffYears = t.getFullYear() - d.getFullYear();
var diffMonths = t.getMonth() - d.getMonth();
var diffDays = t.getDate() - d.getDate();
return diffYears > 18 ? true : diffYears > 17 && diffMonths > -1 && diffDays > -1;
}

function validateForm(f) {
var errormsg = "";
var focusfield;
if (!isDate(f.month.options[f.month.selectedIndex].value,f.day.options[f.day.selectedIndex].value,f.year.options[f.year.selectedIndex].value)) {
errormsg += "* Invalid date of birth\n";
if (!focusfield) focusfield = "day";
}
if (!isOfLegalAge(f.month.options[f.month.selectedIndex].value,f.day.options[f.day.selectedIndex].value,f.year.options[f.year.selectedIndex].value)) {
errormsg += "* You must be at least 18 years of age\n";
}
if (errormsg != "") {
alert("The following errors were encountered:\n\n" + errormsg);
if (focusfield) eval('f.' + focusfield + '.focus()');
return false;
}
else {
return true;
}
}

// -->
</script>
</head>
<body>
<form id="dateform" action="birthdate.htm" method="get" onsubmit="return validateForm(this)">
<table>
<tr>
<td>Date of Birth: </td>
<td>
<script type="text/javascript">
//<![CDATA[

/* Get today's date */
var t = new Date();
var mm = t.getMonth();
var dd = t.getDate();
var yyyy = t.getFullYear();

/* Create an array of month names */
var monthnames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

/* Create month select */
var monthselect = "";
monthselect += '<select name="month">';
for (var i = 0; i < 12; i++) {
monthselect += '<option value="' + (i + 1) + '"';
if (i == mm) {
monthselect += ' selected="selected"';
}
monthselect += '">' + monthnames[i] + '</option>';
}
monthselect += '</select>';

/* Create day select */
var dayselect = "";
dayselect += '<select name="day">';
for (var i = 0; i < 31; i++) {
dayselect += '<option value="' + (i + 1) + '"';
if (i + 1 == dd) {
dayselect += ' selected="selected"';
}
dayselect += '>' + (i + 1) + '</option>';
}
dayselect += '</select>';

/* Create year select */
var yearselect = "";
yearselect += '<select name="year">';
for (var i = yyyy; i > yyyy - 101; i--) {
yearselect += '<option value="' + i + '"';
if (i == yyyy - 18) {
yearselect += ' selected="selected"';
}
yearselect += '>' + i + '</option>';
}
yearselect += '</select>';

/* Write the selects */
document.write(monthselect + dayselect + yearselect);

//]]>
</script>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</td>
</tr>
</table>
</form>
</body>
</html>

P.S. That doesn't necessarily mean there is a "bug" in the date objects, it probably just means that:


<pseudocode>
function snafu() {
return (milliseconds * minutes * hours * days in a year) != (a true year);
}
</pseudocode>


Would always return true...

...but maybe it is a bug. I dunno. I have a very high aptitude for math according to every test I've taken, but I don't really enjoy it much, therefore I don't know as much about it as I should.

I enjoy programming because it's not just math, it's mostly logic. As you can tell, I also enjoy posting verbose messages on the internet. LOL... just kidding. I thought it would be clearer if you saw what I was trying to do in the first place and the solution I ended up with. :eek:

;)

brothercake
02-16-2003, 03:27 AM
Maybe it's because a year isn't exactly that many seconds ... it's about 365 days and 6 hours, which is why we have leap years, to catch up :)

whammy
02-16-2003, 03:35 AM
That's about what I figured... which means that there still "is" a bug in the built-in javascript date methods as well, since setDate() doesn't account for leap years. That explains why it would be about 5 days off or so for someone with my birthdate...

;)

brothercake
02-16-2003, 04:11 AM
Is it a bug, or is it forward compatibility? Maybe in 10 million years it will be different ...

You could divide by 365.25 .. but you might miss people who's 18th birthday was less than six hours ago !

whammy
02-16-2003, 05:00 AM
Sounds good to me! LOL

Obviously it's a base incompatibility... I actually determined EXACTLY what you just said through some testing with milliseconds, which was why I decided to use the method above, since our "year" apparently isn't compatible with milliseconds over a long period (or perhaps a relatively short period) of time.

I had to resort to a simple solution, but it works... besides, isn't that pretty much what I said? :)


<pseudocode>
function snafu() {
return (milliseconds * minutes * hours * days in a year) != (a true year);
}
</pseudocode>