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

How to Calculate total of minutes using JavaScript

Explorer ,
Jun 17, 2024 Jun 17, 2024

 

Hello, I don't do javascript here and while I tried searching.....I am not sure if any of the results I landed on pertain to me. Here are the fields I have.

 

Start Time

End Time

Total Time (In minutes)

 

The start/end times are military time format.  Just need the Total Time filed calculated and displayed in minutes. 

TOPICS
JavaScript
4.0K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Jun 17, 2024 Jun 17, 2024

Assuming the values in Start Time and End Time follow this format:  15:31, enter this custom calculation script in the Total Time field:

 

var startHour=this.getField("Start Time").value.split(":")[0];
var startMin=this.getField("Start Time").value.split(":")[1];
var endHour=this.getField("End Time").value.split(":")[0];
var endMin=this.getField("End Time").value.split(":")[1];


event.value=Number(60*(endHour-startHour)) + Number(endMin-startMin);

View solution in original post

Translate
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 ,
Jun 17, 2024 Jun 17, 2024

Please post the exact format of Start Time and End Time.

Translate
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 ,
Jun 17, 2024 Jun 17, 2024

Assuming the values in Start Time and End Time follow this format:  15:31, enter this custom calculation script in the Total Time field:

 

var startHour=this.getField("Start Time").value.split(":")[0];
var startMin=this.getField("Start Time").value.split(":")[1];
var endHour=this.getField("End Time").value.split(":")[0];
var endMin=this.getField("End Time").value.split(":")[1];


event.value=Number(60*(endHour-startHour)) + Number(endMin-startMin);

Translate
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 ,
Jun 17, 2024 Jun 17, 2024

This also assumes the end time is on the same date as the start time. If the start time is 23:50 and the end time is 00:25, it won't work correctly.

Translate
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 Beginner ,
Jul 02, 2024 Jul 02, 2024
LATEST

how would you do it if start time is 23:50 and end time is 00:25?

Translate
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
Explorer ,
Jun 18, 2024 Jun 18, 2024

HH:MM.   And I don't know if it matters, but any hours entered would be during the day time.  So there should never be a situation where someone is putting in times around midnight. 

Translate
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 ,
Jun 18, 2024 Jun 18, 2024

My script should work then.

Translate
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
Explorer ,
Jun 18, 2024 Jun 18, 2024

Yes it does! Thank you.    How do I resolve the "The value entered does not match the format of the field" error?  I searched and found this to try in the custom validation script section. But it doens't seem to work.  I just want all the fields to display as zero until it needs to do calcuations. 

 

if(event.value == 0)event.value = "";

 

 

I need something to the effect of...if the total fields are 0, then null them out?

Translate
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 ,
Jun 18, 2024 Jun 18, 2024

Set the field's Format setting to None.

Translate
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
Explorer ,
Jun 18, 2024 Jun 18, 2024

I did try that right after my last post.  But now it is showing "NaN" in the total minutes field. 

Translate
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 ,
Jun 18, 2024 Jun 18, 2024

> But now it is showing "NaN" in the total minutes field. 

That means the values of the fields are not what you expect them to be.

For example, when one of the fields is empty the code will not work properly.

Try this:

 

var startTimeString = this.getField("Start Time").valueAsString;
var endTimeString = this.getField("End Time").valueAsString;
if (startTimeString=="" || endTimeString=="") event.value = "";
else {
	var startHour=startTimeString.split(":")[0];
	var startMin=startTimeString.split(":")[1];
	var endHour=endTimeString.split(":")[0];
	var endMin=endTimeString.split(":")[1];
	event.value=Number(60*(endHour-startHour)) + Number(endMin-startMin);
}

 

Translate
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
Explorer ,
Jun 18, 2024 Jun 18, 2024

Correct I should have clarified that it only shows when fields are empty.  I have 10 rows of start/end time fields.  In probably most situations not all of them will be used. But obviously I don't want NaN to show so the form will look cleaner.

 

MidEvil2023_0-1718719093614.png

 

Translate
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 ,
Jun 18, 2024 Jun 18, 2024

When you split a string by a separator (":") into an array and that separator does not exist, then you call an index greater than 0 ("[1]"), it will return "undefined".  When you force "undefined" into a number ("Number(...)") it will return "NaN" (not a number).  From an end user perspective, you should either validate the start and end time fields to the required format, or go with separate dropdowns for hours and minutes.  try67 's script will remove the NaN's when the field is blank. 

Translate
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 ,
Jun 18, 2024 Jun 18, 2024

This should not happen with the new code I provided.

Translate
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
Explorer ,
Jun 18, 2024 Jun 18, 2024

Alright, I got in these each of the fields and adjusted the names.  Then the calucations were not updating and I finally found where the dang calculation order menu was hidden!   I think I got it so far. There's one last piece to this form, but I will make a separate post on it.  Thank you so much. 

Translate
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 Beginner ,
Jun 28, 2024 Jun 28, 2024

How would you then convert minutes to decimal hours?

 

Capture.PNG

Translate
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 ,
Jun 28, 2024 Jun 28, 2024

Divide minutes by 60.

Translate
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 Beginner ,
Jun 28, 2024 Jun 28, 2024

Duh. How do I add the code? This doesn't work: Capture.PNG

Translate
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 ,
Jun 28, 2024 Jun 28, 2024

 

To clarify, divide the TOTAL minutes by 60.  Move /60 outside the last bracket.

Translate
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 Beginner ,
Jun 28, 2024 Jun 28, 2024

Yay!! I'm sure there's code to do it, but I just formatted the cell to number with two decimal points.

 

Thank You!

 

Capture.PNG

Translate
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 ,
Jun 28, 2024 Jun 28, 2024

Yes.  The code would be (add this at the end):

event.value=Math.round(event.value*100)/100;

Translate
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 ,
Jun 28, 2024 Jun 28, 2024

Try this:

var startTimeString = this.getField("TimeIn1").valueAsString;
var endTimeString = this.getField("TimeOut1").valueAsString;

if (startTimeString && endTimeString) {
 var [startHour, startMin] = startTimeString.split(":").map(Number);
 var [endHour, endMin] = endTimeString.split(":").map(Number);
 var diffMinutes = (endHour * 60 + endMin) - (startHour * 60 + startMin);
 event.value = diffMinutes / 60;} 
else
 event.value = "";
Translate
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 ,
Jun 18, 2024 Jun 18, 2024

This might work better from an end user stand point if you have two dropdown fields for each time:  One for Hours (0-23) and another for minutes (0-59), with a colon between them.

Translate
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
Explorer ,
Jun 18, 2024 Jun 18, 2024

But would this affect calculations?  

Translate
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 ,
Jun 18, 2024 Jun 18, 2024

Instead of splitting the string by the : you would get the numbers from the start hours, end hours, start minutes, and end minutes fields.

Translate
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