Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Time into decimal float in Google Sheets using Script?

I want to convert the time HH:MM into H.xx

Like I am getting it in this format: Sat Dec 30 00:00:00 GMT+05:21 1899

But this value is 04:29 in cell. I want it to be 4.5 hours to multiply it to hourly rate.

like image 916
TheOnlyAnil Avatar asked Apr 05 '18 14:04

TheOnlyAnil


People also ask

What is 7 hours and 15 minutes as a decimal?

This online tool will help you convert time given in hours and minutes to decimal hours and/ or decimal minutes. 7 hours 15 minutes is 7.25 hours or 435 minutes.

How do I convert time to decimal in Excel?

The easiest way to convert time to decimal in Excel is to multiply the original time value by the number of hours, seconds or minutes in a day: To convert time to a number of hours, multiply the time by 24, which is the number of hours in a day.


1 Answers

Google Sheets

In Google Sheets, if you have a date/time value in a cell (e.g. "D9"), then use =HOUR(D9)+(MINUTE(D9)/60).

If the value is stored in the format 04:29, then use =INDEX(SPLIT(D9, ":"), 1) + (INDEX(SPLIT(D9, ":"), 2)/60).


Google Sheets API & Google Apps Script

If you want to use the Google Sheets API or Google Apps Script, then you can use javascript.

You need to use the getMinutes() method and divide by 60, then add that to the hour (using getHours()).

var date = new Date(); var minutes = date.getMinutes(); var output = date.getHours() + (minutes/60); 

Be aware that this is ignoring seconds.

If the value in the cell is stored as a string like 04:29, then you'll need to split it.

var time = "04:29"; var hour = Number(time.split(":")[0]); var minutes = Number(time.split(":")[1]); var output = hour + (minutes/60); 
like image 180
Diego Avatar answered Sep 19 '22 09:09

Diego