• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Javascript for time between two time fields in pdf HH:MM

New Here ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

Hi all, 

Got a pdf with fillable fields, TIME OUT & TIME IN. Both are entered as HH:MM. No seconds, no date. 24hr clock. I want to find time difference between them also dispalyed in HH:MM. ie time in 12:00, time out 14:30....elapsed time...02:30. I cant get a head of this and its bugging me. Can anyone help? 

TOPICS
Acrobat SDK and JavaScript

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

This issue was discussed many (many) times on these forums, including full codes to do it. Have you tried searching around a bit?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

Sort of, but some are with YYYY, or HHMMSS, I'm a basic IT person. Can you point me in the direction ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

Just adjust the time format to the one you use, then... It doesn't have to contain the date part.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

Alright, i'll have a search around

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

would this work?

 

var start = document.getElementById("start").value;
var end = document.getElementById("end").value;

function diff(start, end) {
start = start.split(":");
end = end.split(":");
var startDate = new Date(0, 0, 0, start[0], start[1], 0);
var endDate = new Date(0, 0, 0, end[0], end[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);

return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}

document.getElementById("diff").value = diff(start, end);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

The basic code seems fine, although I didn't test it, but it was written for an HTML page, not a PDF file.

Change all instances of document.getElementById(...) to this.getField(...) .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

so this?

 

var start = this.getField("TimeOut") ..value;
var end = this.getField("TimeIN") ..value;

function diff(start, end) {
start = start.split(":");
end = end.split(":");
var startDate = new Date(0, 0, 0, start[0], start[1], 0);
var endDate = new Date(0, 0, 0, end[0], end[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);

return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}

this.getField("diff") ..value = diff(start, end);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 17, 2020 Jul 17, 2020

Copy link to clipboard

Copied

No, it should be followed by only one period, not two, and remove the space before the period.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2020 Jul 19, 2020

Copy link to clipboard

Copied

Like this?
 
var start = this.getField("TimeOut").value;
var end = this.getField("TimeIN").value;
function diff(start, end) {
start = start.split(":");
end = end.split(":");
var startDate = new Date(0, 0, 0, start[0], start[1], 0);
var endDate = new Date(0, 0, 0, end[0], end[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);

return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}
this.getField("diff").value = diff(start, end);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 19, 2020 Jul 19, 2020

Copy link to clipboard

Copied

That seems about right, yes.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 24, 2020 Jul 24, 2020

Copy link to clipboard

Copied

LATEST

Hi sir

 

Yep that works perfect. Working great. see here.

var start = this.getField("Take Off Z1").value; var end = this.getField("Landing Z1").value; function diff(start, end) { start = start.split(":"); end = end.split(":"); var startDate = new Date(0, 0, 0, start[0], start[1], 0); var endDate = new Date(0, 0, 0, end[0], end[1], 0); var diff = endDate.getTime() - startDate.getTime(); var hours = Math.floor(diff / 1000 / 60 / 60); diff -= hours * 1000 * 60 * 60; var minutes = Math.floor(diff / 1000 / 60); return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes; } this.getField("Flight Time1").value = diff(start, end);

 

however, in my fields I have Nan:Nan. How to get rid of that in my code? Also, I have formula in thr 'Fuel Used' whicy us dep fuel - arr fuel...that works fine. In that field, I can''t edit/type anything in (as expected). However in the Flight Time field (nan:nan), it will let me type in text over the top? Why isnt this cell 'blocked' off too? 

 

If I put all times as 'none' formatting, I get NAN. If I put all to TIME HH:MM, then when the fields are empty, it says "

The value entered does not match the format of the field [ Flight Time1 ]" 


What am I doing wrong here, and how to remove the nan:nan from my code? 

Thanks. pic below. 

 

Also I Screenshot 2020-07-24 at 16.22.18.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines