idleberg - 20 days ago 4

Javascript Question

I'm too lazy to fill out my time sheet at work by end at the end of every month, so I've started adding some functions to our PDF form. Acrobat Pro offers making advanced calculations using JavaScript, but I'm stuck with this problem.

I have two fields in which I enter the time when I start/end working. I want to calculate my overtime and output the result in a third field. however, I want the output to be decimal, so when I make half an hour overtime, the result would be 0.5

Example: my worktime is 8.5 hours, I start a 7.30 and finish at 16.00 (4pm).

My code so far:

`var workTime = this.getField ("Work time").value;`

var startTime = this.getField ("Start time").value;

var endTime = this.getField ("End time").value;

event.value = workTime - (endTime - startTime);

Answer

Separate hours and minutes, divide minutes by `60`

, add to hours.

```
function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hours = parseInt(hoursMinutes[0], 10);
var minutes = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hours + minutes / 60;
}
```

Source (Stackoverflow)

Comments