Skip to main content
first.officer
Inspiring
September 28, 2024
Answered

Adding a time value/increment to a clock time, to result in a new clock time (24hr format)

  • September 28, 2024
  • 1 reply
  • 742 views

Hi all,

 

See attached .pdf for the example, would kindly ask if anyone can assist? I am trying to sum(+) a time amount (value), in this case "19:00" - nineteen hours - to an actual clock time that the user puts in (in this case "06:00" - a.m.), so that in this instance, the answer would be equaling . The fields in question are;

 

"CalculationOneTotalC1A" - this is set to the "19:00" - nineteen hours value in the example.

+

"FDPReportTimeC1" - this is set to the "06:00" - a.m. value in the example.

 

"LatestOnBlocksC1A" - this is where the answer of the two fields summed above, should go - in "HH:MM" 24-hr clock format, e.g. in the example it would be a result of "01:00" - a.m., and not "25:00".

 

It's been a while since I have done anything much .pdf related, so very rusty and cannot resolve!....many thanks.

This topic has been closed for replies.
Correct answer Nesa Nurani

Try this:

var timeOne = this.getField("CalculationOneTotalC1A").valueAsString;
var timeTwo = this.getField("FDPReportTimeC1").valueAsString;

var timeToMins = function(timeStr) {
 var parts = timeStr.split(":");
 return (parseInt(parts[0], 10) * 60) + parseInt(parts[1], 10);};

var minsToTime = function(mins) {
 var hours = ("0" + Math.floor(mins / 60) % 24).slice(-2);
 var minutes = ("0" + mins % 60).slice(-2);
 return hours + ":" + minutes;};

event.value = (!timeOne || !timeTwo) ? "" : minsToTime(timeToMins(timeOne) + timeToMins(timeTwo));

Since you format the field as 99:99 how do you know is it am or pm?

Also, since user can enter more than 24h how will you know, for example 19:00 + 06:00 is 01:00 but 19:00 + 30:00 is also 01:00.

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
September 29, 2024

Try this:

var timeOne = this.getField("CalculationOneTotalC1A").valueAsString;
var timeTwo = this.getField("FDPReportTimeC1").valueAsString;

var timeToMins = function(timeStr) {
 var parts = timeStr.split(":");
 return (parseInt(parts[0], 10) * 60) + parseInt(parts[1], 10);};

var minsToTime = function(mins) {
 var hours = ("0" + Math.floor(mins / 60) % 24).slice(-2);
 var minutes = ("0" + mins % 60).slice(-2);
 return hours + ":" + minutes;};

event.value = (!timeOne || !timeTwo) ? "" : minsToTime(timeToMins(timeOne) + timeToMins(timeTwo));

Since you format the field as 99:99 how do you know is it am or pm?

Also, since user can enter more than 24h how will you know, for example 19:00 + 06:00 is 01:00 but 19:00 + 30:00 is also 01:00.

first.officer
Inspiring
September 29, 2024

Hi Nesa,

 

As always, many thanks for the assistance - worked like a charm! really grateful for your help.

 

And yes - hadn't considered the >24hr point, will have ti think about that and work something out in the event it exceeds, the "99:99" (arbitrary mask) was more to allow me to put in times without the need for use of the ":" colon....just me being lazy lol.