Page 1 of 3

calculating time elapsed?

PostPosted: Tue Dec 16, 2014 8:23 am
by yanzco
a bit off topic, or i just forgot my highschool math...

i want to calculate the time difference in 2 times..

12pm to 1pm is 1hour..
12pm - 3pm is 3hours..
1am to 3pm is 14hours..

so whats the formula?

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 10:08 am
by Gaev
yanzco:

What is the range of the two times ? ... same day, across midnight, more than one day ?

What is the granularity ? ... just hours or hours and minutes or hours and minutes and seconds ?

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 10:26 am
by yanzco
Gaev wrote:yanzco:

What is the range of the two times ? ... same day, across midnight, more than one day ?

What is the granularity ? ... just hours or hours and minutes or hours and minutes and seconds ?


range of 2 times?.. within24hours
granularity?.. hours and minutes..

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 10:31 am
by Gaev
yanzco:

So what is the format of the two "date+time" fields used in the calculation ? ... provide exact examples.

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 10:36 am
by yanzco
im trying to make it work with just

time a = 3:00pm
time b = 5:15pm
time elapsed = time c = 2hr15min

if it would be easier to calculate in 24hr format then im fine with that too....

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 10:37 am
by fkapnist
Break it down to milliseconds to do your computations... But remember it is not base-10 math here

60000 = 1 min
120000 = 2 min
180000 = 3 min
240000 = 4 min
---------------------
hex | base 10


I would remove the AM and PM part for the calculation. Use the 24:00 hour time instead..... measure the difference in milliseconds between the two times, and convert the value back to hours and minutes using something like the above table ...

:!:

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 10:43 am
by yanzco
fkapnist wrote:Break it down to milliseconds to do your computations... But remember it is not base-10 math here

60000 = 1 min
120000 = 2 min
180000 = 3 min
240000 = 4 min
---------------------
hex | base 10


:!:


sorry fkapnist.. i do not know how to .. with that...

@gaev

i guess i will be using (H:M:S AM/PM) using the predefined variable [time] to record time

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 10:49 am
by fkapnist
yanzco wrote:
fkapnist wrote:Break it down to milliseconds to do your computations... But remember it is not base-10 math here

60000 = 1 min
120000 = 2 min
180000 = 3 min
240000 = 4 min
---------------------
hex | base 10


:!:


sorry fkapnist.. i do not know how to .. with that...

@gaev

i guess i will be using (H:M:S AM/PM) using the predefined variable [time] to record time



Just use the 24:00 hour time instead..... measure the difference in milliseconds between the two times. That's it. Once you've gotten that far, you can display the time difference value in any granularity you want...

.

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 11:01 am
by yanzco
how do i convert 0200 in millisecond
or 14:25 to millisecond??



predefined global variable
[hour] its 1 , or 12

how can i make it show 01 or 12

(with a zero if 1-9)

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 11:11 am
by fkapnist
The AM and PM parts are not used to calculate the difference. Using a 24 hour format sample below, just subtract and the answer is in hours:

19:24:42 (7:24 PM)


08:24:42 (8:24 AM)
------------

11:00:00


First find out which time is larger with something like...

If "[time1]" ">" "[time2]"
subtract time2 from time1
else
subtract time1 from time2

any value you get will be the time difference


:arrow:
.

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 11:23 am
by yanzco
fkapnist wrote:The AM and PM parts are not used to calculate the difference. Using a 24 hour format sample below, just subtract and the answer is in hours:

19:24:42 (7:24 PM)


08:24:42 (8:24 AM)
------------

11:00:00


First find out which time is larger with something like...

If "[time1]" ">" "[time2]"
subtract time2 from time1
else
subtract time1 from time2

any value you get will be the time difference


:arrow:
.



i get an error with

Code: Select all
If "[timea]" ">" "[timeb]"
Math "[timeb]-[timea]" "2" "[timec]"
else
Math "[timea]-[timeb]" "2" "[timec]"


how do i do what you were trying to make me do? :O

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 12:53 pm
by fkapnist
Sorry, I removed this post until I can get the PUB to work correctly....

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 4:48 pm
by yanzco
if i check elapsed time between 7:45:00 to 8:24:00 it says its 79:00
correct answer should be 39 minutes


if i check 7:45 (bob time) to 9:24 (mary time)
it says 1hr 79min

correct answer should be 1hr 39min

hmmmm..

so confused, how do you get it..

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 4:59 pm
by fkapnist
I also found this Javascript that does exactly what you want:

Syntax: [ Download ] [ Hide ]
Using Javascript Syntax Highlighting
<html>
<head>
<script language="javascript" type="text/javascript">
function p (i)
{
        return Math.floor(i / 10) + "" + i % 10;
}

function init ()
{
        var form = document.getElementById('form');
        var date = new Date();
        var s = p(date.getMonth() + 1) + "/" + p(date.getDate()) + "/" + date.getFullYear() + " " + p(date.getHours()) + ":" + p(date.getMinutes()) + ":" + p(date.getSeconds());
        if (form.date1.value == "")
                form.date1.value = s;
        if (form.date2.value == "")
                form.date2.value = s;
}

function trunc (i)
{
        var j = Math.round(i * 100);
        return Math.floor(j / 100) + (j % 100 > 0 ? "." + p(j % 100) : "");
}

function calculate (form)
{
        var date1 = new Date(form.date1.value);
        var date2 = new Date(form.date2.value);
        var sec = date2.getTime() - date1.getTime();
        if (isNaN(sec))
        {
                alert("Input data is incorrect!");
                return;
        }
        if (sec < 0)
        {
                alert("The second date ocurred earlier than the first one!");
                return;
        }

        var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;

        form.result_h.value = trunc(sec / hour);
        form.result_m.value = trunc(sec / minute);
        form.result_s.value = trunc(sec / second);

        var days = Math.floor(sec / day);
        sec -= days * day;
        var hours = Math.floor(sec / hour);
        sec -= hours * hour;
        var minutes = Math.floor(sec / minute);
        sec -= minutes * minute;
        var seconds = Math.floor(sec / second);
        form.result.value = days + " day" + (days != 1 ? "s" : "") + ", " + hours + " hour" + (hours != 1 ? "s" : "") + ", " + minutes + " minute" + (minutes != 1 ? "s" : "") + ", " + seconds + " second" + (seconds != 1 ? "s" : "");
}
</script>
</head>

<body>
<center>
<table border="0" cellpadding="1" cellspacing="0" id="mainbg"><tr><td>

</td>
</tr>
</table>
<br>
</center>
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td width="500" valign="top" class="content">
<div align="center">
<span class="red">Days, hours, minutes, and seconds between two dates</span>
</div>

        <form id="form">
        <table cellpadding="3">
                <tr>
                        <td colspan="2" align="center">Enter values in <font color="#800000">mm/dd/yy hh:mm:ss</font> format</td>
                </tr>
                <tr>
                        <td>First date and time</td>
                        <td><input type="text" name="date1" /></td>
                </tr>
                <tr>
                        <td>Second date and time</td>
                        <td><input type="text" name="date2" /></td>
                </tr>
                <tr>
                        <td colspan="2" align="center"><input type="button" name="submit" value="Calculate" onclick="calculate(this.form)" /></td>
                </tr>
                <tr>
                        <td>Result</td>
                        <td colspan="2"><input type="text" name="result" readonly="readonly" size="40" /></td>
                </tr>
                <tr>
                        <td>In (decimal) hours</td>
                        <td colspan="2"><input type="text" name="result_h" readonly="readonly" /></td>
                </tr>
                <tr>
                        <td>In (decimal) minutes</td>
                        <td colspan="2"><input type="text" name="result_m" readonly="readonly" /></td>
                </tr>
                <tr>
                        <td>In seconds</td>
                        <td colspan="2"><input type="text" name="result_s" readonly="readonly" /></td>
                </tr>
        </table>
        </form>
<script language="JavaScript" type="text/javascript">
<!--
init();
//-->
</script>

</td></tr></table>
</body>
</html>
 
Parsed in 0.022 seconds, using GeSHi 1.0.8.10


Save it as a html page or paste it into a [BrowserLoadFromStr] object in Neobook to see how it works...

:arrow:
.

Re: calculating time elapsed?

PostPosted: Tue Dec 16, 2014 5:07 pm
by yanzco
other programming languages has its own way to solve it..

wonder if neobook has its own way for calculating time..