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

Getting and illegal character in a work hrs calculation

New Here ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

I am getting an illegal character error in line 7.  I'm trying to determine the number of hours worked between two time, including if the time cross over midnight.  Can you review and let me know what the error is?

 

if ((this.getField("End time").value.length == 0) || (this.getField("Start time").value.length == 0)) {
event.value = " 24.0";
}
else{

var time1 = this.getField(“Text1”).value;
var time2 = this.getField(“Text2”).value;

// convert to date
var datetime1 = new Date(‘1970/01/01 ‘ + time1);
var datetime2 = new Date(‘1970/01/01 ‘ + time2);

var diffInMilliSeconds = Math.abs(datetime1 – datetime2) / 1000;

// calculate hours
var hours = Math.floor(diffInMilliSeconds / 3600) % 24;
diffInMilliSeconds -= hours * 3600;

// calculate minutes
var minutes = Math.floor(diffInMilliSeconds / 60) % 60;
diffInMilliSeconds -= minutes * 60;

// set field value to the difference
event.value =hours + “:” +minutes;


var result;

if (endMin < startMin) {
var minutesPerDay = 24*60;
result = minutesPerDay - startMin; // Minutes till midnight
result += endMin; // Minutes in the next day
} else {
result = endMin - startMin;
}

var minutesElapsed = result % 60;
var hoursElapsed = (result - minutesElapsed) / 60;

alert ( "Elapsed Time : " + hoursElapsed + ":" + (minutesElapsed < 10 ?
'0'+minutesElapsed : minutesElapsed) ) ;

TOPICS
PDF forms

Views

888

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 ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

Sorry, Text1 and Text2 should be Start Time and End Time

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 ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

Only use this quotes "" not these “”

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 ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

That did correct that illegal character for line 7 but now I have one a line 14.

 

if ((this.getField("End time").value.length == 0) || (this.getField("Start time").value.length == 0)) {
event.value = " 24.0";
}
else{

var time1 = this.getField("End Time").value;
var time2 = this.getField("Start Time").value;

// convert to date
var datetime1 = new Date('1970/01/01 ' + time1);
var datetime2 = new Date('1970/01/01 ' + time2);

var diffInMilliSeconds = Math.abs(datetime1 – datetime2) / 1000;

// calculate hours
var hours = Math.floor(diffInMilliSeconds / 3600) % 24;
diffInMilliSeconds -= hours * 3600;

// calculate minutes
var minutes = Math.floor(diffInMilliSeconds / 60) % 60;
diffInMilliSeconds -= minutes * 60;

// set field value to the difference
event.value =hours + ":" +minutes;


var result;

if (endMin < startMin) {
var minutesPerDay = 24*60;
result = minutesPerDay - startMin; // Minutes till midnight
result += endMin; // Minutes in the next day
} else {
result = endMin - startMin;
}

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 ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

In this line: var diffInMilliSeconds = Math.abs(datetime1 datetime2) / 1000;

minus/subtraction sign is not correct.

Also, at first 'else' you opened curly brackets but didn't close it.

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 ,
Feb 08, 2023 Feb 08, 2023

Copy link to clipboard

Copied

Seems to be working but it is not putting the data in the form field.  Have I missed a step?

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 ,
Feb 13, 2023 Feb 13, 2023

Copy link to clipboard

Copied

I had some help in adjusting the code but it is still not placing the data in the PDF field.  Is there something still wrong with the code?

 

if ((this.getField("End Time").value.length == 0) || (this.getField("Start Time").value.length == 0)) {
event.value = "24.0";
} else {
var endTime = this.getField("End Time").value;
var startTime = this.getField("Start Time").value;

// convert to date
var endDate = new Date('1970/01/01 ' + endTime);
var startDate = new Date('1970/01/01 ' + startTime);

var diffInMilliSeconds = Math.abs(endDate - startDate) / 1000;

// calculate hours
var hours = Math.floor(diffInMilliSeconds / 3600) % 24;
diffInMilliSeconds -= hours * 3600;

// calculate minutes
var minutes = Math.floor(diffInMilliSeconds / 60) % 60;

// set field value to the difference
var formattedTime = endDate.toLocaleTimeString("en-US", {hour: '2-digit', minute:'2-digit'});
event.value = formattedTime;

// calculate elapsed time
var elapsedTime = ((hours + 24) - (startDate.getHours() + (startDate.getMinutes() / 60))) % 24;
var hoursElapsed = Math.floor(elapsedTime);
var minutesElapsed = Math.floor((elapsedTime - hoursElapsed) * 60);

alert("Elapsed Time: " + hoursElapsed + ":" + (minutesElapsed < 10 ? '0' + minutesElapsed : minutesElapsed));

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 ,
Feb 13, 2023 Feb 13, 2023

Copy link to clipboard

Copied

LATEST

-Where did you place the code?

-What's the point of the code after this line:

event.value = formattedTime;

?

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