Page 3 of 3

Re: calculating time elapsed?

PostPosted: Thu Apr 28, 2016 10:14 am
by Wrangler
Instead of finding the difference between 2 times, I'm trying to subtract 3.5 hours from any time during the day. Ex: 20:40:00 or 11:15:00. Can anyone help with a jscript that will do this? The result should be in 24 hour format and stored in a neobook variable.

Thanks in advance for any help.

Re: calculating time elapsed?

PostPosted: Thu Apr 28, 2016 10:27 am
by Gaev
Wrangler:

What result do you expect when you subtract 3.5 hours from 02:20:11 ? .... in other words, does the date come into play ? ... or is the answer just 22:50:11 ?

Re: calculating time elapsed?

PostPosted: Thu Apr 28, 2016 10:43 am
by Wrangler
Hi Gaev,

Thanks for the response.

The date isn't that important. I guess it would only fall into play if the calculation was done around midnite, like 1:00 in the morning. That subtraction would put it into the day before. I could live without it if it complicates things too much.

Re: calculating time elapsed?

PostPosted: Thu Apr 28, 2016 10:46 am
by Wrangler
Riley: The forum doesn't send out an email when a subscribed topic is responded to.

Re: calculating time elapsed?

PostPosted: Thu Apr 28, 2016 11:15 am
by stu
My quick solution, disgusting as it might be would go like:
Use vbs to convert date string ("28/04/2016 13:54:00") to epoch, epoch - ((Hours * 3600) + (minutes * 60))
and then epoch back to readable date time... with all well documented limitations of unix time =)

Re: calculating time elapsed?

PostPosted: Thu Apr 28, 2016 1:56 pm
by Gaev
Wrangler:

Try this out with different valyes for fromTime and subtractTime ...
Code: Select all
var fromTime = "3:30:01";
var subtractTime = "3:31:00";

fromTimeArray = fromTime.split(":");
fromTimeHH = Number(fromTimeArray[0]);
fromTimeMM = Number(fromTimeArray[1]);
fromTimeSS = Number(fromTimeArray[2]);

subtractTimeArray = subtractTime.split(":");
subtractTimeHH = Number(subtractTimeArray[0]);
subtractTimeMM = Number(subtractTimeArray[1]);
subtractTimeSS = Number(subtractTimeArray[2]);

answerTimeSS = fromTimeSS - subtractTimeSS;

if (answerTimeSS < 0)
{
   answerTimeSS = 60 + answerTimeSS;
   answerTimeMM = fromTimeMM - subtractTimeMM - 1;
}
else
{
   answerTimeMM = fromTimeMM - subtractTimeMM;   
}

if (answerTimeMM < 0)
{
   answerTimeMM = 60 + answerTimeMM;
   answerTimeHH = fromTimeHH - subtractTimeHH - 1;
}
else
{
   answerTimeHH = fromTimeHH - subtractTimeHH;
}

if (answerTimeHH < 0)
{
   answerTimeHH = 24 + answerTimeHH;
}

//make a string
if (answerTimeHH < 10)
{
   answerTimeHHString = "0" + answerTimeHH.toString();
}
else
{
   answerTimeHHString = answerTimeHH.toString();
}

if (answerTimeMM < 10)
{
   answerTimeMMString = "0" + answerTimeMM.toString();
}
else
{
   answerTimeMMString = answerTimeMM.toString();
}

if (answerTimeSS < 10)
{
   answerTimeSSString = "0" + answerTimeSS.toString();
}
else
{
   answerTimeSSString = answerTimeSS.toString();
}

//now join them
answerTimeHMS = answerTimeHHString + ":" + answerTimeMMString + ":" + answerTimeSSString;

alert(answerTimeHMS);


If you need explanation for commands, ask here ... if numbers are not as expected, provide values of input, expected, actual and various interim variables (you can use alert command to obtain them).

Re: calculating time elapsed?

PostPosted: Thu Apr 28, 2016 2:45 pm
by Wrangler
Thanks, Gaev. Looks like I won't be at the keyboard for a couple days. Will test when I get back and let you know how it goes.

Thanks again.

Re: calculating time elapsed?

PostPosted: Fri Apr 29, 2016 11:01 am
by Wrangler
Ok. Got a little time.

Tried the script with this:

BrowserExecScript "browser1" "var fromTime = [#34]3:30:01[#34];| var subtractTime = [#34]3:31:00[#34];|| fromTimeArray = fromTime.split([#34]:[#34]);| fromTimeHH = Number(fromTimeArray[0]);| fromTimeMM = Number(fromTimeArray[1]);| fromTimeSS = Number(fromTimeArray[2]);|| subtractTimeArray = subtractTime.split([#34]:[#34]);| subtractTimeHH = Number(subtractTimeArray[0]);| subtractTimeMM = Number(subtractTimeArray[1]);| subtractTimeSS = Number(subtractTimeArray[2]);|| answerTimeSS = fromTimeSS - subtractTimeSS;|| if (answerTimeSS < 0)| {| answerTimeSS = 60 + answerTimeSS;| answerTimeMM = fromTimeMM - subtractTimeMM - 1;| }| else| {| answerTimeMM = fromTimeMM - subtractTimeMM;| }|| if (answerTimeMM < 0)| {| answerTimeMM = 60 + answerTimeMM;| answerTimeHH = fromTimeHH - subtractTimeHH - 1;| }| else| {| answerTimeHH = fromTimeHH - subtractTimeHH;| }|| if (answerTimeHH < 0)| {| answerTimeHH = 24 + answerTimeHH;| }|| //make a string| if (answerTimeHH < 10)| {| answerTimeHHString = [#34]0[#34] + answerTimeHH.toString();| }| else| {| answerTimeHHString = answerTimeHH.toString();| }|| if (answerTimeMM < 10)| {| answerTimeMMString = [#34]0[#34] + answerTimeMM.toString();| }| else| {| answerTimeMMString = answerTimeMM.toString();| }|| if (answerTimeSS < 10)| {| answerTimeSSString = [#34]0[#34] + answerTimeSS.toString();| }| else| {| answerTimeSSString = answerTimeSS.toString();| }|| //now join them| answerTimeHMS = answerTimeHHString + [#34]:[#34] + answerTimeMMString + [#34]:[#34] + answerTimeSSString;|| alert(answerTimeHMS);" "JScript"

Don't see any output at all. Also, can I set the var 'fromtime' with a neobook variable somehow? And how to display the result in a neobook variable?

Re: calculating time elapsed?

PostPosted: Fri Apr 29, 2016 11:25 am
by Gaev
Wrangler:

Don't see any output at all.

The answer is in the Javascript variable named answerTimeHMS ... hence the (last) command alert(answerTimeHMS);.

You could replace it with the NeoBook command to save that in a NeoBook variable.

Also, can I set the var 'fromtime' with a neobook variable somehow? And how to display the result in a neobook variable?

Yes, you can ... See the Help file section titled Passing Information Between the Browser and NeoBook.


If you need this functionality within NeoBook, wouldn't it be easier to have a NeoBook Function/SubRoutine that did it all without involving Javascript ? ... NeoBook has better commands to split the hour:min:sec string and stitch the hour/minute/second components of the answer into another NeoBook variable ... the Math is a wash between the two languages ... let me know your preference.

Re: calculating time elapsed?

PostPosted: Fri Apr 29, 2016 11:36 am
by Wrangler
If you need this functionality within NeoBook, wouldn't it be easier to have a NeoBook Function/SubRoutine that did it all without involving Javascript ?


I'm all for easier. I guess I did ask for jscript. My bad. Should have left that up to you. I have no idea how to turn it into a function, but I'm all for it if you are. :)

Thanks, Gaev.

Re: calculating time elapsed?

PostPosted: Fri Apr 29, 2016 12:19 pm
by Gaev
Wrangler:

Calling code ...
Code: Select all
SetVar "[fromTime]" "20:40:00"
SetVar "[subtractTime]" "11:15:00"
GoSub "DoSubtractTime"
AlertBox "answerTimeHMS" "[answerTimeHMS]"

Subroutine code ...
Code: Select all
:DoSubtractTime
StrParse "[fromTime]" ":" "[fromTimeArray]" "[notImportant1]"
StrParse "[subtractTime]" ":" "[subtractTimeArray]" "[notImportant2]"

Math "[fromTimeArray3] - [subtractTimeArray3]" "0" "[answerTimeArray1]"
If "[answerTimeArray3]" "<" "0"
   Math "60 + [answerTimeArray3]" "0" "[answerTimeArray3]"
   Math "[fromTimeArray2] - [subtractTimeArray2] - 1" "0" "[answerTimeArray2]"
Else
   Math "[fromTimeArray2] - [subtractTimeArray2]" "0" "[answerTimeArray2]"   
EndIf

If "[answerTimeArray2]" "<" "0"
   Math "60 + [answerTimeArray2]" "0" "[answerTimeArray2]"
   Math "[fromTimeArray1] - [subtractTimeArray1] - 1" "0" "[answerTimeArray1]"
Else
   Math "[fromTimeArray1] - [subtractTimeArray1]" "0" "[answerTimeArray1]"   
EndIf

If "[answerTimeArray1]" "<" "0"
   Math "24 + [answerTimeArray1]" "0" "[answerTimeArray1]"
EndIf

... leading zeroes
If "[answerTimeArray1]" "<" "10"
   SetVar "[answerTimeArray1]" "!0[answerTimeArray1]"
EndIf
If "[answerTimeArray2]" "<" "10"
   SetVar "[answerTimeArray2]" "!0[answerTimeArray2]"
EndIf
If "[answerTimeArray3]" "<" "10"
   SetVar "[answerTimeArray3]" "!0[answerTimeArray3]"
EndIf
...join them
SetVar "[answerTimeHMS" "![answerTimeArray1]:[answerTimearray2]:[answerTimeArray3]"

Return

Re: calculating time elapsed?

PostPosted: Fri Apr 29, 2016 1:14 pm
by Wrangler
Didn't work. Then removed bracket: SetVar "[answerTimeHMS" "![answerTimeArray1]:[answerTimearray2]:[answerTimeArray3]"
Now works.

Result: 09:24:60

Shouldn't that be 09:25:00?

Re: calculating time elapsed?

PostPosted: Fri Apr 29, 2016 1:47 pm
by Wrangler
Never mind. Figured that one out on my own:

If "[answerTimeArray3]" "=" "60"
SetVar "[answerTimeArray3]" "!00"
SetVar "[answerTimeArray2]" "[answerTimeArray2]+1"
EndIf

As always, you da man, Gaev. Thank you for taking the time to put this together for me. I'm not much with math, or javascript.

Re: calculating time elapsed?

PostPosted: Fri Apr 29, 2016 5:01 pm
by Gaev
Wrangler:
Didn't work. Then removed bracket: SetVar "[answerTimeHMS" "![answerTimeArray1]:[answerTimearray2]:[answerTimeArray3]"
Now works.

Result: 09:24:60

Shouldn't that be 09:25:00?

Instead of removing the bracket, it should be paired off with a closing one (like so) ...
Code: Select all
SetVar "[answerTimeHMS]" "![answerTimeArray1]:[answerTimearray2]:[answerTimeArray3]"


Never mind. Figured that one out on my own:

If "[answerTimeArray3]" "=" "60"
SetVar "[answerTimeArray3]" "!00"
SetVar "[answerTimeArray2]" "[answerTimeArray2]+1"
EndIf

You shouldn't need this code ... as I can't see how [answerTimeArray3] can be 60 ...
Code: Select all
Math "[fromTimeArray3] - [subtractTimeArray3]" "0" "[answerTimeArray3]"
If "[answerTimeArray3]" "<" "0"
   Math "60 + [answerTimeArray3]" "0" "[answerTimeArray3]"
   Math "[fromTimeArray2] - [subtractTimeArray2] - 1" "0" "[answerTimeArray2]"
Else
   Math "[fromTimeArray2] - [subtractTimeArray2]" "0" "[answerTimeArray2]"
EndIf

... unless you entered 60 for the seconds part in [fromTime] ... or the If command has "=" comparator instead of "<".

The logic is that if after subtraction, the seconds component is negative ... then you have to ...

- add 60 seconds to the seconds result (i.e. take one minute from the minutes component)
- subtract 1 minute from the minutes component (to account for the 60 seconds being taken)

Just tried those same values on my pub ... returns 09:25:00 (without your code).

Re: calculating time elapsed?

PostPosted: Fri Apr 29, 2016 6:36 pm
by virger
Quiza ayude, lo uso desde 2014.
Maybe it'll help, I use since 2014.

Code: Select all
{NeoBook 5 Objects}
NeoBookVer=5.80
ObjectType=8
Name=Info
X=0
Y=1
W=340
H=104
Anchor=0
Font=Arial
FontSize=12
FontStyle=3
FontCharset=1
TextColor=0
Text={\rtf1\ansi\deff0\deftab254{\fonttbl{\f0\fnil\fcharset1 Arial;}}{\pard{\ql\li0\fi0\ri0\sb0\sl\sa0 \plain\f0\fs24\cf0\b\i \tab \tab \tab \plain\f0\fs24\cf0\b\ul dd/mm     hh:mm     am.pm\plain\f0\fs24\cf0\b\i \tab \tab \tab \tab \par\ql\li0\fi0\ri0\sb0\sl\sa0 \plain\f0\fs24\cf0\b\i \tab \tab Dia #1\tab \tab \tab \tab \tab \tab \tab \tab \tab Dia#2}}}
HMargin=0
VMargin=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16119285
FillPattern=0
TabOrder=10
ObjectType=9
Name=eH1
X=59
Y=60
W=53
H=33
Anchor=0
Text=11:59
VarName=[vh1]
Align=1
EditMask=!90:00
EditLen=5
LineColor=0
LineWidth=1
LineStyle=0
FillColor=13684984
FillPattern=0
Font=Arial
FontSize=12
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=1
ObjectType=9
Name=eH2
X=239
Y=60
W=53
H=33
Anchor=0
Text=00:01
VarName=[vh2]
Align=1
EditMask=!90:00
EditLen=5
LineColor=0
LineWidth=1
LineStyle=0
FillColor=13684984
FillPattern=0
Font=Arial
FontSize=12
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=5
ObjectType=13
Name=RbPm1
X=116
Y=79
W=41
H=21
Anchor=0
Text=pm
Align=1
VarName=[sap1]
InitState=0
AutoSize=Yes
LineColor=0
LineWidth=1
LineStyle=0
FillColor=13684984
FillPattern=0
Font=Arial
FontSize=11
FontStyle=1
FontCharset=1
TextColor=0
TabOrder=3
ObjectType=13
Name=RbAm1
X=116
Y=56
W=40
H=21
Anchor=0
Text=am
Align=1
VarName=[sap1]
InitState=1
AutoSize=Yes
LineColor=0
LineWidth=1
LineStyle=0
FillColor=13684984
FillPattern=0
Font=Arial
FontSize=11
FontStyle=1
FontCharset=1
TextColor=0
TabOrder=2
ObjectType=13
Name=RbPm2
X=296
Y=79
W=41
H=21
Anchor=0
Text=pm
Align=1
VarName=[sap2]
InitState=1
AutoSize=Yes
LineColor=0
LineWidth=1
LineStyle=0
FillColor=13684984
FillPattern=0
Font=Arial
FontSize=11
FontStyle=1
FontCharset=1
TextColor=0
TabOrder=7
ObjectType=13
Name=RbAm2
X=296
Y=56
W=40
H=21
Anchor=0
Text=am
Align=1
VarName=[sap2]
InitState=0
AutoSize=Yes
LineColor=0
LineWidth=1
LineStyle=0
FillColor=13684984
FillPattern=0
Font=Arial
FontSize=11
FontStyle=1
FontCharset=1
TextColor=0
TabOrder=6
ObjectType=9
Name=eD1
X=2
Y=60
W=53
H=33
Anchor=0
Text=03/03
VarName=[vd1]
Align=1
EditMask=!90/90
EditLen=5
LineColor=0
LineWidth=1
LineStyle=0
FillColor=14743776
FillPattern=0
Font=Arial
FontSize=12
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=0
ObjectType=9
Name=eD2
X=182
Y=60
W=53
H=33
Anchor=0
Text=04/03
VarName=[vd2]
Align=1
EditMask=!90/90
EditLen=5
LineColor=0
LineWidth=1
LineStyle=0
FillColor=14743776
FillPattern=0
Font=Arial
FontSize=12
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=4
ObjectType=3
Name=BtCalcule
X=0
Y=105
W=340
H=33
Anchor=0
Text=[dd] Dia(s)  [hh] Hr: [mm] Mn
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=.. setvar "[yr]" "2016¶.. setvar "[f1]" "![vd1]/[yr]"¶SetVar "[F1]" "![vD1]/2016"¶DateToNum "[F1]" "d/m/y" "[nD1]"¶If "[nD1]" "=" "Error"¶    AlertBox "ATENCION" "ERROR EN FECHA #1"¶    Gotoline "NoCalcula"¶EndIf¶¶SetVar "[F2]" "![vD2]/2016"¶DateToNum "[F2]" "d/m/y" "[nD2]"¶If "[nD2]" "=" "Error"¶    AlertBox "ATENCION" "ERROR EN FECHA #2"¶    Gotoline "NoCalcula"¶EndIf¶¶If "[nD1]" ">" "[nD2]"¶    SetVar "[x]" "[nD1]"¶    SetVar "[nD1]" "[nD2]"¶    SetVar "[nD2]" "[x]"¶EndIf¶¶StrParse "[vH1]" ":" "[H1]" "[x]"¶IfEx "[H11]>11 OR [H12]>59"¶    Alertbox "ERROR" "ATENCION A LA HORA/MINUTO #1"¶    Gotoline "NoCalcula"¶EndIf¶If "[sAp1]" "=" "am"¶    SetVar "[Ap1]" "0"¶Else¶    SetVar "[Ap1]" "12"¶EndIf¶Math "([nD1]*60*24)+(([H11]+[Ap1])*60)+[H12]" "" "[M1]"¶¶StrParse "[vH2]" ":" "[H2]" "[x]"¶IfEx "[H21]>11 OR [H22]>59"¶    Alertbox "ERROR" "ATENCION A LA HORA/MINUTO #2"¶    Gotoline "NoCalcula"¶EndIf¶If "[sAp2]" "=" "am"¶    SetVar "[Ap2]" "0"¶Else¶    SetVar "[Ap2]" "12"¶EndIf¶Math "([nD2]*60*24)+(([H21]+[Ap2])*60)+[H22]" "" "[M2]"¶¶Math "[M2]-[M1]" "" "[Tm]"¶¶.. math "(60*24)" "" "[fc]"¶.. math "trunc([tma]/[fc])" "" "[dd]"¶Math "Trunc([Tm]/(60*24))" "" "[DD]"¶Math "[Tm]-([DD]*(60*24))" "" "[Tm]"¶¶Math "Trunc([Tm]/60)" "" "[HH]"¶¶Math "[Tm]-([HH]*60)" "" "[MM]"¶¶Math "Abs([DD])" "" "[DD]"¶Math "Abs([HH])" "" "[HH]"¶Math "Abs([MM])" "" "[MM]"¶:NoCalcula
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12644600
FillPattern=0
Font=Arial
FontSize=14
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=8
ObjectType=7
Name=Line1
X1=2
Y1=47
X2=338
Y2=46
EndCaps=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16119285
FillPattern=0
TabOrder=12
ObjectType=7
Name=Line2
X1=170
Y1=103
X2=170
Y2=21
EndCaps=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=12644600
FillPattern=0
TabOrder=11

Ajustelo a su necesidad
Fits your need