Script to substract date time format to get result in hours - google-apps-script

I have two cell that have time and date in this format "9/12/2021 10:41:571".
I need help with writing a function that will return the difference of both the timestamp in hours

As long as your date/times are formatted as numbers rather than text, you could subtract them from each other then multiply by 24:
=(A2-A1)*24

If the number of hours does not exceed 24, you can also use the following formulae:
=HOUR(A3-A2)
=MINUTE(A3-A2)
=SECOND(A3-A2)
=CONCATENATE(B2," hour/s ",C2," minute/s, ",D2," second/s")
Therefore, assuming that your two dates are located in the A2 and A3 cells, the formulae above will calculate the difference between the two returning the hours, minutes and seconds.
Spreadsheet
Reference
HOUR;
MINUTE;
SECOND;
CONCATENATE.

Related

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.
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);
Just Multiply your Duration by 24.
Example:
Time In | Time out | Duration | Decimal
11:45:00 AM | 3:40:00 PM | 3:55:00 | 3.92
You also have to alter the format of the cell containing the decimal from Automatic to Number in order for this to work in Google Sheets.
All you need is TO_PURE_NUMBER()
https://support.google.com/docs/answer/3094243?hl=en
Example:
// The following
A1=DATEVALUE("2020-05-18 06:09:18.821 ")+TIMEVALUE("2020-05-18 06:09:18.821 ")
A2=TO_PURE_NUMBER(A1)
A3=TO_DATE(A2)
// Results in the following, first result is just a formatted date
2020-05-18 6:09:19
43969.25647
5/18/2020
I tried the above methods with limited success, as I needed to have another cell to convert the time value to after calculation. However, I tried out a modification of this, and to my surprise, it works. I'll share my solution and hope it helps.
I have a Start Time and an End Time. Let's say my Start Time is in D6 and my End Time is in E6.
With the above formulas, I would have to have a cell (let's say F6) which does E6-D6, then another cell (let's say G6) to convert F6 from time to decimals.
I short-cut this by using:
=INDEX(SPLIT(E6-D6, ":"), 1) + (INDEX(SPLIT(E6-D6, ":"), 2)/60)
directly into F6 without the need for a G6 cell to convert.
Hope this helps!
=(INDEX(SPLIT(E17, ":"), 1) &"."& (INDEX(SPLIT(E17, ":"), 2))) * hourlyRate
This seemed to work:
=TEXT(D2-C2,"h:mm")*24
Where D2 is End Time and C2 is Start Time,
The =TEXT(D2-C2,"h:mm") part both subtracts the times and converts the result into time format. The * 24 part makes it into a usable decimal instead of time format. So, in my case, I had Start Time as 12:30 pm and End Time as 1:20 pm. The Sheets was seeing the difference as :50, which it was interpreting as 12:50am. But when you multiply by 24, it then works out, giving you .833333333.
I tested it with Start Time 10am and End Time 12pm and got 2.000000000 which is also correct.
Oops I see now you wanted it using script. Sorry. This is for within sheets.
First of all - this thread is sure useful. It took me a couple of answers to understand what am I supposed to do, So.. I'm sharing my experience + images.. yay
What you should do:
Select format --> number --> automatic (see image below)
Insert your hours values (each value in a different cell)
Subtract time values to represent the difference between them (in time format)
Use timevalue function to represent the the difference as a decimal number
Multiply the value with 24 in order to get the decimal representation in hours
Here's an image that displays the values I've got
And here's the formulas (assuming that time values are in columns A and B)
Column C --> B - A for C1 formula is B1-A1, C2 formula is B2-A2 etc.
Column D --> timevalue(C) for D1 formula is timevalue(C1), D2 formula is timevalue(C2) etc.
Column E --> D * 24 for E1 formula is D1 * 24, E2 formula is D2 * 24 etc.
or if you prefer this as a single formula
timevalue(B - A) * 24
Hope that I've clarified myself enough and that your are going to solve this even if you are not a software developer. Cheers

hh:mm duration format only

I am using Google Sheets to collect some data I need a cell to only allow a duration hh:mm input.
Here is the formula data validation I have right now
=regexmatch(text(E11,"hh:mm"), "[0-1][0-9]:[0-5][0-9]$")
The only thing this is not doing for me is not allowing whole number responses (i.e. 1,3,5..). These values break some formulas that I have as 7 = 168 hours in duration. What can I do to limit only to hh:mm format entry?
I am willing to use a script if this is what is needed.
=REGEXMATCH(TO_TEXT(E11),"\d{1,2}:[0-5]\d?")
TO_TEXT gets display values instead of number.
\d{1,2} adds a condition for a 1 to 2 digit number.

MYSQL time diff

I'm trying to calculate time diff between two time fields. Because the fields are just time with no date, I can't use timestampdiff, so I'm using timediff() or subtime(). The only problem with those is when the second time is less than the first time, it returns a negative timediff for me. I understand it's taking the times as the same day times, but is there any way to get a behavior where it always calculates the time forward? For example, I have 23:00:00 and 07:00:00.
timediff('07:00:00','23:00:00') will return a negative value, because 23:00 is greater than 07:00, but I want to get 8 hours as the return. Is there any way I can do that?
You can achieve that with an if statement:
select if(dateA > dateB,
timediff(dateA, dateB),
addtime(timediff(dateA, dateB), '24:00:00.000000'))
This way, if the first date is smaller than the second, you add 24 hours to the difference.

How can I get Mysql's DATE_FORMAT() to display hours over 24?

I'm using DATE_FORMAT(time, '%H:%i') to get the time nicely formatted in HH:MM in a query, my problem however is that if the time goes over 24 hours it adds a day and the hour count will start again from zero, this way I can't display times like 40:00.
I have looked at the documentation here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
But there doesn't seem to be an obvious way to display hours over 24, the only choices seem to be 00..23 and 01..12, I'd be happy with something like 01..99 instead, is there a way to do this?
Try using TIME_FORMAT() instead of DATE_FORMAT(). TIME_FORMAT()'s documentation says that
If the time value contains an hour part that is greater than 23, the %H and %k hour format specifiers produce a value larger than the usual range of 0..23. The other hour format specifiers produce the hour value modulo 12

What time does it signify?

I created a table with one attribute tt and inserted a value into it.
CREATE TABLE tt(tm TIME);
INSERT INTO tt VALUES(2342342);
On executing the select command the result shown is in the form :
234:23:42
What time does this signify ?
MySQL retrieves and displays TIME values in HH:MM:SS format or HHH:MM:SS format for large hour values. The reason why it can have large values is because it can also represent an interval between two events (which may span over multiple days for example, or even be negative).
H stands for hour, M for minute, and S for second.
So, when you insert 2342342 it becomes 234:23:42 representing 234 hours, 23 minutes, and 42 seconds.
Reference
MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or
'HHH:MM:SS' format for large hours values). TIME values may range from
'-838:59:59' to '838:59:59'. The hours part may be so large because
the TIME type can be used not only to represent a time of day (which
must be less than 24 hours), but also elapsed time or a time interval
between two events (which may be much greater than 24 hours, or even
negative).
http://dev.mysql.com/doc/refman/5.0/en/time.html
MySQL recognizes TIME values in these formats:
As a string in 'D HH:MM:SS' format. You can also use one of the
following “relaxed” syntaxes: 'HH:MM:SS', 'HH:MM', 'D HH:MM', 'D HH',
or 'SS'. Here D represents days and can have a value from 0 to 34.
As a string with no delimiters in 'HHMMSS' format, provided that it
makes sense as a time. For example, '101112' is understood as
'10:11:12', but '109712' is illegal (it has a nonsensical minute part)
and becomes '00:00:00'.
As a number in HHMMSS format, provided that it makes sense as a time.
For example, 101112 is understood as '10:11:12'. The following
alternative formats are also understood: SS, MMSS, or HHMMSS.
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-literals.html