Extract Minutes from Time Delta: How-To
- Remaining minutes
- min
- Result
- As H:MM
- Result
- Total minutes
- 150 min
- You entered
This guide explains how to pull the minutes component out of a time delta (a duration) in programming and spreadsheet tools. You’ll learn the direct methods for Python, JavaScript, Excel, and Google Sheets, plus the common pitfalls that lead to wrong numbers.
Formula
To extract minutes from a time delta, you first separate whole hours from the remainder. The formula is:
hours = floor(total_minutes / 60)
minutes = total_minutes mod 60
Where:
- total_minutes – the duration expressed in minutes (integer or decimal).
- floor() – rounds down to the nearest whole number.
- mod – the remainder after division (e.g., 150 mod 60 = 30).
This works for any duration, whether you start with seconds, milliseconds, or a date-time difference.
Worked Example
Using the default of 150 total minutes:
- Divide 150 by 60: 150 ÷ 60 = 2.5
- Floor the result: floor(2.5) = 2 hours
- Multiply hours back: 2 × 60 = 120
- Subtract from total: 150 − 120 = 30 minutes
So 150 minutes = 2 hours and 30 minutes, shown as 2:30.
Why Extract Minutes from a Time Delta?
Time deltas appear everywhere: calculating work hours, video durations, cooking times, or elapsed time between two events. Often you need the minutes part separately for display (e.g., “2h 30m”) or for further calculation (e.g., billing by the minute). Direct extraction avoids manual conversion errors.
How to Do It in Python
Python’s datetime.timedelta stores days, seconds, and microseconds. To get minutes, convert everything to seconds first:
total_seconds = td.total_seconds() minutes = int(total_seconds // 60) % 60
For a duration of 2.5 hours, td.total_seconds() gives 9000, then 9000 // 60 = 150, and 150 % 60 = 30. If you have a plain number of minutes, use divmod(minutes, 60) which returns (hours, minutes).
How to Do It in JavaScript
JavaScript doesn’t have a built-in time delta type, so you work with milliseconds. Given two Date objects:
const diffMs = date2 - date1; const totalMinutes = Math.floor(diffMs / 60000); const minutes = totalMinutes % 60;
For a 2.5-hour difference, diffMs is 9,000,000 ms, totalMinutes is 150, and minutes is 30. If you start with seconds, divide by 60 instead.
How to Do It in Excel and Google Sheets
In Excel and Google Sheets, time is stored as a fraction of a day. To extract minutes from a time value or a duration:
- If A1 contains a time (e.g., 2:30), use
=MINUTE(A1)to get 30. - If A1 contains a decimal number of hours (e.g., 2.5), use
=MOD(A1*60, 60)to get 30. - If A1 contains a text duration like “2:30”, convert with
=MINUTE(TIMEVALUE(A1)).
For a duration that exceeds 24 hours, MINUTE still works correctly because it extracts the minute component of the time portion.
Common Mistakes and How to Avoid Them
People often trip on these:
- Using modulo on hours instead of minutes. For 2.5 hours,
2.5 % 1gives 0.5, but that’s half an hour, not 30 minutes. Always convert to minutes first. - Ignoring negative deltas. If the end time is before the start time, the delta is negative. Decide whether you want absolute value or a signed result.
- Rounding errors with floating point. In JavaScript,
0.1 + 0.2is not exactly 0.3. UseMath.round()on the total minutes before extracting. - Forgetting that Excel time is a fraction of a day. Multiplying a time by 24 gives hours, but multiplying by 1440 gives minutes.
Frequently Asked Questions
How do I get minutes from a time delta in pandas?
In pandas, if you have a Timedelta column, use .dt.total_seconds() // 60 to get total minutes, then apply modulo 60 to get the minutes part. For example, df['minutes'] = df['delta'].dt.total_seconds().astype(int) // 60 % 60.
What’s the difference between total minutes and minutes part?
Total minutes is the entire duration expressed in minutes (e.g., 150 minutes for 2.5 hours). The minutes part is the remainder after removing whole hours (e.g., 30). Use total minutes for calculations, and the minutes part for display.
Can I extract minutes from a time delta in SQL?
Yes, most SQL dialects have functions. In PostgreSQL, use EXTRACT(MINUTE FROM interval) to get the minutes component. In MySQL, MINUTE(timediff(end, start)) works. For total minutes, use TIMESTAMPDIFF(MINUTE, start, end).
Why does my JavaScript code give 30 minutes for 2.5 hours?
Because 2.5 hours is 150 minutes, and 150 % 60 = 30. That’s correct. If you expected 0.5, you’re thinking of hours as a decimal, not minutes. To get decimal hours, divide total minutes by 60.
How do I format minutes as “Xh Ym” in Python?
Use f"{hours}h {minutes}m" after extracting hours and minutes with divmod(total_minutes, 60). For example, divmod(150, 60) returns (2, 30), which formats to “2h 30m”.