One column is labeled Checkin Time and the other is labeled Checkout Time and finally I have a column that is labeled Total Time Worked. The cells in the Total Time worked column should subtract the time in the checkout time column from the time in the Checkin Time column. How do I do this in the script? The times in the Checkin and Checkout cells are in the military time format which I got from the timestamp upon the form submission.
Google-sheets – How to subtract two time values in military format in Google Apps Scripts for a spreadsheet
google sheetsgoogle-apps-script
Related Topic
- Google Sheets – Adding Timestamps to Two Different Columns
- Google Sheets – Subtract Values in a List That Fit Certain Criteria
- Google-sheets – In Google Sheets, how does one get a 12-hour time of day *without* an AM/PM indicator
- Google Apps Script – Run Locally to Process Spreadsheet Data
- Google-sheets – Entering mm:ss: milliseconds into Google Sheets for time duration (arithmetic)
- Replacing Line Breaks in Google Sheets – How to Replace Multiple Line Breaks in a Cell
- Google-sheets – Help needed with script that auto-updates cells in column in Google Spreadsheet showing last modify date
- Google Sheets – Fix Date and Time Formatting with Google Apps Script
Best Answer
Here's a script that should do what you need:
The
militaryTimeDiff
takes as arguments two time values in military format (HHMM) and returns the total number of minutes between them.You can use it in your spreadsheet formula like this (replace the cell references with the correct ones):
Note: this only works correctly for values within the same day. If you have, for example start time = 2350 and end time = 0125, the function will not realize that the times represent two different days and will return a negative difference.