hh:mm duration format only - google-apps-script

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.

Related

How can I change a custom date into numbers in Google spreadsheet?

I want to do a vlookup/sumif based on dates
How can I do it? I saw from a source I have to first change it into numbers
Somehow I cannot do it with formulas like value/Datevalue, I can only convert it by changing its format.
Spreadsheet image
The vlookup() and sumif() functions work fine with dates and date time values.
With vlookup(), you can get an exact match of a date time value by setting the is_sorted parameter to false. To match the first value that is less than or equal to a date time, make sure that the data range is sorted, and set that parameter to true.
With sumif(), if you need to remove the time component from a date time value to match dates to date times, use datevalue().
See this answer for an explanation of how dates and times work in spreadsheets.

How do I split this datestamp in Google Sheets

I have a datestamp in this format on C6:
21/05/2021 10:41:35 PM
How can I split this into date and time so that I have date in D6 and time in E6?
I want to do this because I have browsing history I want to import to calendar.
I found several answers to this question with various answers but none of them worked for me.
You can use SPLIT(C6, " ") to split contents of the cell.
This splits the time and AM/PM too, but you can join it in another cell using JOIN(" ", E6:F6)
date in Google Sheets is always an integer, and time is a fractional number, so you can divide the date and time very easily
Since your data comes from an imported .csv file, it could be formatted as text.
If that is the case, try the following formula
=SPLIT(REGEXREPLACE(A1,"(.*\/\d+) ","$1#"),"#")
You can then format the results to your liking.
(As always, do adjust ranges and locale as needed)
Here is a simple option that you can use as long as each datestamp is exactly the same:
Use the LEFT() and RIGHT() functions. (These also work in Excel)
For the date, use LEFT(DATESTAMP_CELL,10). This will return the first 10 characters from the cell, which in this case are the date "21/05/2021".
For the time, use RIGHT(DATESTAMP_CELL,11). This will return the last 11 characters from the cell, which in this case are the time "10:41:35 PM".
This should be the result:

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

Convert Date to a number in mysql (Like how a date converts in Excel)

What is the correct function to use when i want to convert a date to a number. Like when you enter a date in excel for example (now()) and then it will show the date but when you click on the number format it will show you a number.
I tried to use Unix timestamp but its not exactly the output i was looking for.
The date i entered is today's date
=now()
and the output i was hoping to get is
42146
what's the correct function to get this result in mysql?
Thank you
Microsoft Excel bases date serial numbers from Jan 1, 1904 or from Jan 1, 1900 (depends on the setting in the workbook.)
To generate a date "serial number" similar to what Excel uses, just calculate the number of days between NOW() (or whatever date you want to convert), and the base date. For example:
SELECT DATEDIFF(NOW(),'1900-01-01') AS excel_serial_date_1900
(You might need to add 1 or subtract 1 to get the exact same value that Excel uses, I've not tested that.) If your Excel workbook is using the 1904 based serial date, use '1904-01-01' as the base date in the expression.
Note: the DATEDIFF function returns integer number of days, operates only on the "date" portion, and doesn't include any fraction of a day.
Reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff

Format Excel, column to get sum of hh:mm:ss when they are listed that way

I need to get the total duration time in hours:minutes:seconds when the list of time is all in one column. The seconds need to convert into minutes and seconds when they exceed 60 and the minutes into minutes and hours. I am getting the original data from a CVS format so it must all go into one column. I need to use a formula that will give me the sum for each person's individual times.
For example: a column like 01:22:03 added to 00:58:57 must come out to 02:21:00. I have tried putting the format into custom time h:mm:ss but it does not work. I have far too many to add on a weekly basis to do it by hand and I know someone has an answer for this new Excel user.
use the timevalue() function to convert a string of hh:mm:ss into a serial number that can be summed and displayed as a hh:mm:ss number.
Add all the cells with AutoSum.
All you need do is apply an appropriate format to its result:
Select the cell containing the total
From the Format menu, choose the Cells command
From the Category list, select Custom
In the Type box, input the format [h]:mm
(note the use of square brackets).
If you want to show seconds in your total, input [h]:mm:ss
(this format is listed in Excel's Custom formats)
Click OK.
all the best
If they are all stored as actual times, you should just be able to use a custom format of [hh]:mm:ss (this allows the hour total to go above 24.
If this isn't working then it is likely they are likely stored as text in which case you will need to split them and recombine them either through formulas or using text to columns.
If it is stored as a proper date =isnumber(your cell) should return TRUE, if not it is text.
If you are copying and pasting the data in make sure the cells aren't formatted as text before you do as it could cause issues.
If your format is always with this structure (00:00:00) you can use:
01:22:03(A1) 00:58:57(B1) 0 (C1) 2 (D1) 21(E1) 0 (F1) 02:21:00(G1)
05:22:04(A2) 00:50:40(B2) 0 (C2) 6 (D2) 12(E2) 44(F2) 06:12:44(G2)
11:22:05(A3) 00:08:59(B3) 0 (C3) 11(D3) 31(E3) 4(F3) 11:31:04(G3)
D = horas; E = minutes; F = Seconds
Formulas used:
C1 =(NSHORA(EXTRAE(A2;1;2);EXTRAE(A2;4;2);EXTRAE(A2;7;2)))+NSHORA(EXTRAE(B2;1;2);EXTRAE(B2;4;2);EXTRAE(B2;7;2))
d1 =HORA(C1)
e1 =MINUTO(C1)
f1 =SEGUNDO(C1)
We could finally get in a different column the result with your format if necessary with this one:
=CONCATENAR(SI(LARGO(D2)<2;CONCATENAR(0;D2);D2);":";SI(LARGO(E2)<2;CONCATENAR(0;E2);E2);":";SI(LARGO(F2)<2;CONCATENAR(0;F2);F2))
Select range C1:G1 and copy it to get what you want. After it you could also hide columns c, d, e and f
Since I'm programmer, I use VBA for everything :)
here is little VBA (Macro) for that.
There isn't test for input format so code will break if column do not contains fields like 01:12:13
'' SUM time in Column
'' Choose Column,StartRow,EndRow
'' result will be placed at Column:EndRow + 1
'' Column=1 1=A,2=B, ...
Dim w As Worksheet
Set w = ActiveWorkbook.Worksheets(1) ''choose first worksheet
Dim d As Double
Dim Column, StartRow, EndRow As Integer
Column = 1 ''this is A Column
StartRow = 1
EndRow = 21
For i = StartRow To EndRow
d = d + CDate(w.Cells(i, Column)) * 24 * 60 * 60
Next
''convert seconds to days
d = d / 86400.0
''display result in EndRow+1
w.Cells(EndRow + 1, Column) = Format(d, "hh:mm:ss")
Use the sum product function. I am no wiz I just googled the crap out of it. What happens is that the duration is converted into text so you have to convert the text to value. So for eg
=sumproduct((a2:a50)*1) then to get the duration you have to format the cells to duration and it'll get the number.
This is the site that helped me to figure it out
https://a4accounting.com.au/how-to-sum-text-numbers-in-excel/