This topic is locked
[SOLVED]

button code to calculate time between two times

12/16/2022 8:45:05 AM
PHPRunner General questions
N
nagate816 authorDevClub member

Hello,

Yesterday I got code similar to the code below to work and then copied a bunch of other things around on the form and somehow deleted the code that worked :(

Anyway I have a start time and end time fields and I want to calculate the elapsed time between them.

Here is my button code:

// Put your code here.

var varstart = Runner.getControl(pageid, 'StartTime');
var varend = Runner.getControl(pageid, 'EndTime');
var varhoursworked = Runner.getControl(pageid, 'HoursWorked');

var timestart = new Date("01-01-2020 " + varstart.getValue());
var timeend = new Date("01-01-2020 " + varend.getValue());

varhoursworked.setValue((+timeend.getValue())-(+timestart.getValue()));

// params["txt"] = "Hello";
// ajax.setMessage("Sending request to server...");
// Uncomment the following line to prevent execution of "Server" and "Client After" events.

return false;

This was the last version I saved before I got it to work witrh the changes I thougth I made to get it to work and I know it comes out in miliseconds but at the mement the code above does not seem to do anything. Anyoen see what I'm missing ?

Thanks,

Neil.

Sergey Kornilov admin 12/16/2022

Really hard to tell what might be wrong without having access to the project.

Are there any Javascript errors there? You can see them in the Chrome Developer Tools:
https://xlinesoft.com/phprunner/docs/troubleshooting_javascript_errors.htm

N
nagate816 authorDevClub member 1/10/2023

I worked this out so in case it's hjelpful to others, this code is in the Client Before event oif a button where StartTiem and End Time are Time Fields and HoursWorked is a numeric field. Here's the code that works:

// Put your code here.

var varstart = Runner.getControl(pageid, 'StartTime');
var varend = Runner.getControl(pageid, 'EndTime');
var varhoursworked = Runner.getControl(pageid, 'HoursWorked');

var timestart = new Date("01-01-2020 " + varstart.getValue());
var timeend = new Date("01-01-2020 " + varend.getValue());

varhoursworked.setValue(((timeend-timestart)/60000)/60);

return false;

Thanks,

Neil.