Converting Non-Time String into HH:MM - excel

I have a Cell range that contains data in the following format:
1d 12h 37m
All Data is in this format but ranges from single to double digit values.
I need to try to convert this data as an ultimate goal into purely hh:mm
I am struggling with the conversion of this. The main issue arises when the number of digits varying between double and single digits.
I have the formula to extrapolate purely the numbers and remove the letters from this:
=SUM(ROUND(MOD(SMALL(IFERROR(ROW(OFFSET(A$1,,,LEN(A11)))+MID(A11,1+LEN(A11)-ROW(OFFSET(A$1,,,LEN(A11))),1)%, LEN(A11)+1), ROW(OFFSET(A$1,,,SUM(N(ISNUMBER(0+MID(A11,ROW(OFFSET(A$1,,,LEN(A11))),1))))))),1)*100,0)*(10^(ROW(OFFSET(A$1,,,SUM(N(ISNUMBER(0+MID(A11,ROW(OFFSET(A$1,,,LEN(A11))),1))))))-1)))&""
Ideally this needs to all be done as a formula but a VBA script is okay.
Any ideas?

You can use this formula to change to date/time:
=IFERROR(LEFT(A1,FIND("d",A1)-1),0)+TRIM(SUBSTITUTE(SUBSTITUTE(MID(A1,IFERROR(FIND("d",A1)+1,1),LEN(A1)),"h ",":"),"m",""))
Then either format it [hh]:mm which will return 36:37 or hh:mm which will return 12:37

=TEXT(((LEFT(A2,FIND("d",A2)-1))+((TRIM(MID(A2,FIND("d",A2)+1,FIND("h",A2)-FIND("d",A2)-1)))+((TRIM(MID(A2,FIND("h",A2)+1,FIND("m",A2)-FIND("h",A2)-1)))/60))/24),"[h]:mm")
This formula assumes a couple things:
"d", "h", "m" will always be present
You need Days converted to Hours
Explanation:
Days:
=(LEFT(A2,FIND("d",A2)-1))
Hours:
=(TRIM(MID(A2,FIND("d",A2)+1,FIND("h",A2)-FIND("d",A2)-1)))
Minutes:
=(TRIM(MID(A2,FIND("h",A2)+1,FIND("m",A2)-FIND("h",A2)-1)))
Combining:
=TEXT((<days>+(<hours>+(<minutes>/60))/24),"[h]:mm")
If you want it without Days converted to Hours, change "[h]:mm" to "hh:mm".

Related

how to get zulu time difference seconds/milliseconds in excel

The excel column contains Zulu Timzezones How to calculate the difference between two dates in seconds.milliseconds
From Time 2022-04-25T04:16:57.823121842Z
To Time
2022-04-25T04:16:58.173194593Z
2022-04-25T04:16:58.089133751Z
2022-04-25T04:16:58.462278784Z
2022-04-25T04:16:57.829376293Z
2022-04-25T04:16:57.961790312Z
2022-04-25T04:16:58.445884586Z
2022-04-25T04:16:57.830806273Z
2022-04-25T04:16:58.067723338Z
2022-04-25T04:16:58.470913276Z
2022-04-25T04:16:57.838787068Z
When I Try to Do something like =B13-B14
Error
Function MINUS parameter 1 expects number values. But '2022-04-25T04:35:59.092943356Z' is a text and cannot be coerced to a number.
Converted to Number format
REVISED: I forgot to convert the milliseconds
You can convert the date strings into time values by breaking them into parts:
=DATEVALUE(LEFT(A2,10)) + TIMEVALUE( MID(A2,12,8) ) --MID(A2,20,10)/24/60/60
Where A2 is the date string.
This assumes that they have the exact structure that you have shown and fully padded with zeros. If that is not the case, for example the milliseconds could be .095Z, then you can mod this to:
=DATEVALUE(LEFT(A2,10)) + TIMEVALUE( MID(A2,12,8) ) --MID(SUBSTITUTE(A2,"Z",""),20,999)/24/60/60
to be safe.

How can I format a string into a date: time format?

For example:
2021-08-18T22:24:49-06:00
I want to print this to a more readable format like: 8/18/21 10:24pm
I have tried using the built in DateTime function but it returns an error. Can someone point me in the right direction? I have checked other answers but they all relate to using the aforementioned funciton.
Looking at how your data is formatted and it seems your data is formatted "yyyy-mm-ddThh:mm:ss";so, here is my attempt:
Formula in C1:
=--SUBSTITUTE(LEFT(A1,16),"T"," ")
Then I just formatted the resulting datetime-stamp with:
m/d/yy hh:mm AM/PM
So it remains a numeric value to do your calculations with if needed.
Give a try to below formula-
=TEXT(FILTERXML("<t><s>"&SUBSTITUTE(A1,"T","</s><s>")&"</s></t>","//s[1]")+FILTERXML("<t><s>"&SUBSTITUTE(FILTERXML("<t><s>"&SUBSTITUTE(A1,"T","</s><s>")&"</s></t>","//s[2]"),"-","</s><s>")&"</s></t>","//s[1]"),"M/dd/yyyy hh:mm AM/PM")
The date is in ISO 8601 format. This will parse out the different parts of the date string and convert to a date, assuming that your string is in A1:
=DATEVALUE(LEFT(A1,10))
+TIMEVALUE(MID(A1,12,8))
+TIMEVALUE(RIGHT(A1, 5))
*INT(MID(A1, 20, 1) & 1)
The first part grabs the date, the second part grabs the time, the third part captures the date offset, and the last part captures the sign. If you want to format that, you can do it with the cell formatting or wrap it in TEXT:
=TEXT(
DATEVALUE(LEFT(A1,10))
+TIMEVALUE(MID(A1,12,8))
+TIMEVALUE(RIGHT(A1, 5))
*INT(MID(A1, 20, 1) & 1),
"yyyy-mm-dd hh:mm:ss"
)
Note that if you need to support UTC, that is indicated by Z instead of a time offset, and you would need to modify the formula slightly. If your data always has the same time offset, you could just hardcode it, instead of parsing it out.

SAS: Date reading issue

I have imported an excel sheet where the date1 is 4/1/16 date2 is 5/29/14 and date3 is 5/2/14. However, when I import the sheet into SAS and do PROC PRINT gives the first 2 variable columns as "42461" and "41788" while the date3 is 05/02/2014.
I need these date formats consistent b/c I am doing a Cox regression with PROC PHREG.
Any thoughts about how to make these dates consistent?
Thanks!
This probably depends on how the data is represented in Excel and how it is imported into SAS. First, are the formats the same in Excel? The first two are being imported as a number. The second as a string.
In Excel, you can format the column using a date format. Perhaps your import method will recognize this. You can also define another column as a string, using the text(<whatever>, "YYYY-MM-DD") to convert to a string in that format.
Alternatively, you can import all as numbers and then add the value to 1899-12-31. That is the base date for Excel. This makes more sense if you think of "1" as being 1900-01-01.
Because your column had mixed numeric (date) and character values SAS imported the field as character. So the actual dates got imported as the text version of the actual number that Excel stores for dates. The ones that look like date strings in SAS are the fields that were strings in Excel also.
Or if in your case one of the three columns was all valid dates then SAS imported it as a number and assigned a date format to it so there is nothing to fix for that column.
The best way to fix it is to make sure that all of the values in the date column are either real dates or empty cells. Then PROC IMPORT will be able to make the right guess at how to import it.
Once you have the strings in SAS and you want to try to fix them then you need to decide which strings look like integers and which should be treated as date strings.
So you might just check if they have any non-digit characters and assume those are the ones that are date strings instead of numbers. For the ones that look like integers just adjust the number to account for the fact that Excel numbers dates from 1900 and SAS numbers them from 1960.
data want ;
set have ;
if missing(exel_string) then date=.;
else if notdigit(trim(excel_string)) then date=input(excel_string,anydtdte32.);
else date=input(excel_string,32.) + '01JAN1900'd -2 ;
format date yymmdd10. ;
run;
You might wonder why the minus 2? It is because Excel starts from 1 instead of 0 and also because Excel thinks 1900 was a leap year. Here are the Excel date numbers for some key dates and a little SAS program to convert them. Try it.
data excel_dates;
input datestr :$10. excel_num :comma32. #1 sas_num :yymmdd10. ;
diff = sas_num - excel_num ;
format _numeric_ comma14. ;
sasdate1 = excel_num - 21916;
sasdate2 = excel_num + '01JAN1900'd -2 ;
format sasdate: yymmdd10.;
cards;
1900-01-01 1
1900-02-28 59
1900-03-01 61
1960-01-01 21,916
2018-01-01 43,101
;

How to convert DD:HH:MM to HH:MM in Excel

I am facing a problem with excel.How can I convert DD:HH:MM to HH:MM in excel?
Here is an exmaple showcasing what I need.
1:02:49(1day, 2hours, 49 mins) should come as 26:49(26 hours, 49 min)
I'm guessing that your 1:02:49 cell is formatted as text; otherwise, you could just change the cell format from DD:HH:MM to HH:MM.
I've opted to output as text, but could change it to an actual Time value if needbe.
Yes, the formula is a bit long but it can cope with any number of digits between each :. So, for example, 100:3:4 (100 days, 3 hours, 4 minutes) would correctly evaluate to 2403:04
=24*LEFT(A1,FIND(":",A1)-1)+MID(A1,FIND(":",A1)+1,LEN(A1)-2-LEN(RIGHT(A1,LEN(A1)-FIND("#",SUBSTITUTE(A1,":","#",2))))-LEN(LEFT(A1,FIND(":",A1)-1)))&":"&TEXT(RIGHT(A1,LEN(A1)-FIND("#",SUBSTITUTE(A1,":","#",2))),"00")

How to convert long minutes to hours in excel

I need a formula in excel to convert minutes to hours. Until now came the following formula:
=A1/1440
So, 180minutes its converted to 03:00, when formatted as hh:mm
But, i need also to convert long minutes, like 1600. In this case, the formula converts to 02:40, when it should show 26:40.
Any ideas?
You can try
=INT(A1/60)&":" &MOD(A1,60)
and =TEXT(FLOOR(A1/60,1),"00")&":"&TEXT(MOD(A1,60),"00") for Format hh:mm
Use exactly the same formula you are using now, but change the display format to:
[h]:mm:ss
That will display in the format you want AND allow you to perform operations on the values.
( With format showing in the top of the time columns)
[h]:mm:ss hh:mm:ss Equal?
=1600/1440 =1600/1440 =G23=F23
=F23+1600/1440 =G23+1600/1440 =G24=F24
Gives
[h]:mm:ss hh:mm:ss Equal?
26:40:00 2:40:00 TRUE
53:20:00 5:20:00 TRUE

Resources