Skip to main content
Participating Frequently
February 8, 2023
Question

Getting and illegal character in a work hrs calculation

  • February 8, 2023
  • 1 reply
  • 1568 views

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) ) ;

This topic has been closed for replies.

1 reply

Participating Frequently
February 8, 2023

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

Nesa Nurani
Community Expert
Community Expert
February 8, 2023

Only use this quotes "" not these “”

Participating Frequently
February 8, 2023

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;
}