Source language: Translate to:

calculating time elapsed?

General questions about NeoBook

Moderator: Neosoft Support

calculating time elapsed?

Postby yanzco » Tue Dec 16, 2014 8:23 am

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?
yanzco
 
Posts: 178
Joined: Sun Jul 20, 2014 4:07 am

Re: calculating time elapsed?

Postby Gaev » Tue Dec 16, 2014 10:08 am

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 ?
User avatar
Gaev
 
Posts: 3738
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: calculating time elapsed?

Postby yanzco » Tue Dec 16, 2014 10:26 am

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..
yanzco
 
Posts: 178
Joined: Sun Jul 20, 2014 4:07 am

Re: calculating time elapsed?

Postby Gaev » Tue Dec 16, 2014 10:31 am

yanzco:

So what is the format of the two "date+time" fields used in the calculation ? ... provide exact examples.
User avatar
Gaev
 
Posts: 3738
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: calculating time elapsed?

Postby yanzco » Tue Dec 16, 2014 10:36 am

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....
yanzco
 
Posts: 178
Joined: Sun Jul 20, 2014 4:07 am

Re: calculating time elapsed?

Postby fkapnist » Tue Dec 16, 2014 10:37 am

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 ...

:!:
Last edited by fkapnist on Tue Dec 16, 2014 10:46 am, edited 1 time in total.
User avatar
fkapnist
 
Posts: 348
Joined: Mon Nov 17, 2014 4:24 pm
Location: Greece

Re: calculating time elapsed?

Postby yanzco » Tue Dec 16, 2014 10:43 am

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
yanzco
 
Posts: 178
Joined: Sun Jul 20, 2014 4:07 am

Re: calculating time elapsed?

Postby fkapnist » Tue Dec 16, 2014 10:49 am

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...

.
User avatar
fkapnist
 
Posts: 348
Joined: Mon Nov 17, 2014 4:24 pm
Location: Greece

Re: calculating time elapsed?

Postby yanzco » Tue Dec 16, 2014 11:01 am

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)
yanzco
 
Posts: 178
Joined: Sun Jul 20, 2014 4:07 am

Re: calculating time elapsed?

Postby fkapnist » Tue Dec 16, 2014 11:11 am

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:
.
User avatar
fkapnist
 
Posts: 348
Joined: Mon Nov 17, 2014 4:24 pm
Location: Greece

Re: calculating time elapsed?

Postby yanzco » Tue Dec 16, 2014 11:23 am

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
yanzco
 
Posts: 178
Joined: Sun Jul 20, 2014 4:07 am

Re: calculating time elapsed?

Postby fkapnist » Tue Dec 16, 2014 12:53 pm

Sorry, I removed this post until I can get the PUB to work correctly....
Last edited by fkapnist on Tue Dec 16, 2014 5:17 pm, edited 1 time in total.
User avatar
fkapnist
 
Posts: 348
Joined: Mon Nov 17, 2014 4:24 pm
Location: Greece

Re: calculating time elapsed?

Postby yanzco » Tue Dec 16, 2014 4:48 pm

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..
yanzco
 
Posts: 178
Joined: Sun Jul 20, 2014 4:07 am

Re: calculating time elapsed?

Postby fkapnist » Tue Dec 16, 2014 4:59 pm

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.027 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:
.
User avatar
fkapnist
 
Posts: 348
Joined: Mon Nov 17, 2014 4:24 pm
Location: Greece

Re: calculating time elapsed?

Postby yanzco » Tue Dec 16, 2014 5:07 pm

other programming languages has its own way to solve it..

wonder if neobook has its own way for calculating time..
yanzco
 
Posts: 178
Joined: Sun Jul 20, 2014 4:07 am

Next

Return to General NeoBook Discussions

Who is online

Users browsing this forum: Bing [Bot] and 1 guest