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

# How to Calculate total of minutes using JavaScript

Explorer ,
Jun 17, 2024 Jun 17, 2024

Copied

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

Views

774

Report

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

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

24 Replies 24
Enthusiast ,
Jun 17, 2024 Jun 17, 2024

Copied

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

Report

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

Copied

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

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

Copied

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.

Report

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

Copied

LATEST

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

Report

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

Copied

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.

Report

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

Copied

My script should work then.

Report

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

Copied

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?

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

Copied

Set the field's Format setting to None.

Report

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

Copied

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

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

Copied

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

``````

Report

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

Copied

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.

Report

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

Copied

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.

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

Copied

This should not happen with the new code I provided.

Report

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

Copied

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.

Report

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

Copied

How would you then convert minutes to decimal hours?

Report

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

Copied

Divide minutes by 60.

Report

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

Copied

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

Report

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

Copied

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

Report

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

Copied

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

Thank You!

Report

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

Copied

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

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

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

Copied

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 = "";
``````

Report

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

Copied

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.

Report

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

Copied

But would this affect calculations?

Report

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

Copied

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