How to Convert Time to Decimal

Convert Time to decimal in C#

DateTime dt1 = DateTime.Parse("11:55");    
DateTime dt2 = DateTime.Parse("9:35");

double span = (dt1 - dt2).TotalHours;

Do you actually need the "2:20" or is that just an intermediate step?

Edit: If you wanted to go back, you'd just need to do a little bit of math. Take the remainder of the decimal and multiply by 60, then round. Those will be the minutes, so just add them to the hours.

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

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

Converting Time to decimal

Parsing TimeSpan to decimal does not too much sense because time is not a numeric value at all.

But you can use it's TotalHours property which returns double.

var total = tot_hours.TotalHours;

How can I convert time to decimal number in JavaScript?

Separate hours and minutes, divide minutes by 60, add to hours.

function timeStringToFloat(time) {
var hoursMinutes = time.split(/[.:]/);
var hours = parseInt(hoursMinutes[0], 10);
var minutes = hoursMinutes[1] ? parseInt(hoursMinutes[1], 10) : 0;
return hours + minutes / 60;
}

How to convert time to decimal

There is probably a lubridate function for this, but I'd do it like this:

x <-  "01:18:30"

y <- (as.numeric(as.POSIXct(paste("2014-01-01", x))) -
as.numeric(as.POSIXct("2014-01-01 0:0:0")))/60
#[1] 78.5

Ignoring the hours:

y%%60
#[1] 18.5

Convert hh:mm time to decimal time (Matlab)

you can cast to datenum, take day-fraction from that and multiply by 24. Ex:

f = rem(datenum('08:41', 'HH:MM'), 1)*24 % format is optional
%f =
% 8.6833


Related Topics



Leave a reply



Submit