Differences between string representations of date/times - excel

In Excel 2007, I want the differences for the following string date/times:
A B C
1 Date1 Date2 Difference of A and B
2 2009.11.28 01:25:46:0287 2009.11.28 01:25:46:0287 ?
3 2009.11.28 01:25:46:0443 2009.11.28 01:25:46:0443 ?
I want the differences by converting strings to date/time and then the results as differences of two converted date/times.

A rather long-winded way to calculate zero (for the examples):
=SUBSTITUTE(LEFT(A2,10),".","/")+MID(A2,12,8)+RIGHT(A2,4)/86400000-(SUBSTITUTE(LEFT(B2,10),".","/")+MID(B2,12,8)+RIGHT(B2,4)/86400000)
By special request and very slightly shorter:
=SUBSTITUTE(LEFT(A2,10),".","/")+REPLACE(RIGHT(A2,13),9,1,".")-(SUBSTITUTE(LEFT(B2,10),".","/")+REPLACE(RIGHT(B2,13),9,1,"."))

I couldn't come up with a really nice way of doing this... hopefully someone else will. Having said that, the following may give you what you need.
To convert the main date part, use the following formula (this assumes the string date is in A1):
=DATE(MID(A1,1,4),MID(A1,6,2),MID(A1,9,2)) +
TIME(MID(A1,12,2),MID(A1,15,2),MID(A1,18,2))
To convert the fractional second part, use:
=VALUE(MID(A1,21,4))/10000
The date / time part can easily be subtracted, as can the fractional second part.
The place I ran into trouble was recombining those parts into a whole that Excel will actually display in a sensible way. I finally took the difference between the two dates and multiplied by 86400 (= 24 * 60 * 60 - number of seconds in a typical day) then added the difference in the fractional second part.
Hope this helps.
Regards,
Richard
P.S. There are quite a lot of things I don't like about this solution, the biggest of which is the fragility of the formulas - if the date format changes, the formulas will need to be adjusted.

Related

How to convert two time formats into one?

I am pulling Cross Country race times, and trying to convert the race time into an integer that can be easier to work with.
I am using the formula
=([Cell Name]-INT([Cell Name])*60*24
But I am so far retrieving two different time formats:
17:23.4
15:09
Both formats take that formula differently, and they are producing different numbers. The first format is changed into 17 minutes, but the other is turned into 1000+ minutes. Is there a way that I can convert these times into one format?
The main problem you are having is as tom sharpe pointed out is that some times are being assessed as HH:MM and others as MM:SS. if you look at your source data cell (assmue A2) and use the following formula it will tell you if you are dealing with text or time in excel serial date time formatted to appear in a manner we are used to seeing:
=ISTEXT(A2)
now assuming that the result is TRUE, this means you are dealing with TEXT. Adding a leading 0: to the text will make the time recognizable to excel as being in the format HH:MM:SS and allow for you to deal with it consistently.
To do this use the following formula:
=timevalue("0:"&A2)
Things may get a little wonky if ou have anything greater than or equal to 60 as the first two digits.
once you have it in the proper format you can then use it in your formula.

Determine if calculation between 2 date time values is < 72 Hours in excel

So I'm comparing a booked datetime values between two appointments, and I want to create a flag against each record where the time difference is less than 72 hours.
Calculating the difference in hours is easy using a simple:
=B1-=A1
and then setting the cell format to [h]:mm
However, I've then tried to use an IF statement in a separate column to evaluate and flag if the calculated value is less than or greater than 72hrs:
=IF(C1<72:00:00,1,0)
This, and variations of it using double quotes etc hasn't worked for me.
I guess the problem is because the calculated value in column C is still in datetime format. Can anyone please advise the correct syntax to make the IF statement work?
Excel is actually pretty friendly in this regard. The key is that date/time and differences are reported as decimals - the whole portion being the number of days, the decimal, being the fractional days (hours/minutes).
So - for your example - after doing the subtraction, you should flag that particular field with =C1 < 3 ==> TRUE (if less than 72 hours), FALSE (otherwise)
Hope that helps,
- John
=IF(C1<3,1,0)
72 hours is just the number 3 in Excel Date-Time syntax

Excel time difference

I have two time:
9:29:00 AM (B2)
6:16:00 PM (C2)
I want to get the hh:mm (hours and minutes) between those two times. I tried the following function:
=TEXT(B2-C2,"h:mm")
But I get a #VALUE! error.
I formatted the two time cells as TIME format (*h:mm:ss am:pm) and result cell also as TIME format (hh:mm)
Is that the right way round? Try
=C2-B2
You don't need TEXT function
I believe your #VALUE error is due to your order of subtraction, but I can't be sure without knowing which of those times is B2 and which is C2, and I can't quite comment on your question yet, so an answer it is!
Times in excel are stored as doubles which increment by 1 each day; 0.5 is 12 hours etc.
Best thing to do is use =TEXT(B2-C2,"HH:MM")
This will format B2-C2 (a decimal number) as a time interval.
Of course the contents of B2 and C2 must be numeric; see my VALUE function in the comments.
As barry houdini mentioned back in 2013, I also ran into the error of subtracting giving a negative number, and excel just throws an error. No other response on the internet gave me good results. Although my method is longer, it always works with times in hh:mm:ss AM/PM. Just change the text format part of the formula for whatever you have. My data was:
11:15:45 PM 1:41:20 AM
I converted it to military time for ease with
=TEXT(A2, "hh:mm:ss")
Data was now:
23:15:45 01:41:20
I made another column, let's call it C, that just subtracts the second time from the first time,
=B2-A2
I next used an IF statement on this column, so that if the absolute value of the difference was the same as the number (a positive), I'd use
TEXT(B2-A2,"hh:mm:ss")
which just subtracts the later time from the earlier time. But...
If the absolute value of the difference was not the same, e.g. error-ville with a lot of hashes ##########, I use:
TEXT(24-(A10-B10),"hh:mm:ss")
Altogether, we have one more column, which finally spits out the correct hour difference. This column is predicated on columns A, B, and our error-prone difference column C:
=IF(C2=ABS(C2),TEXT(B2-A2,"hh:mm:ss"),TEXT(24-(A2-B2),"hh:mm:ss"))
this is the most ridiculous thing I have ever done. It's been years and such a simple case, never solved by Excel.

How to get the difference in minutes between two dates in Microsoft Excel?

I am doing some work in Excel and am running into a bit of a problem. The instruments I am working with save the date and the time of the measurements and I can read this data into Excel with the following format:
A B
1 Date: Time:
2 12/11/12 2:36:25
3 12/12/12 1:46:14
What I am looking to do is find the difference in the two date/time stamps in mins so that I can create a decay curve from the data. So In Excel, I am looking to Make this (if the number of mins in this example is wrong I just calculated it by hand quickly):
A B C
1 Date: Time: Time Elapsed (Minutes)
2 12/11/12 2:36:25 -
3 12/12/12 1:46:14 1436.82
I Have looked around for a bit and found several methods for the difference in time but they always assume that the dates are the same. I exaggerated the time between my measurements some but that roll over of days is what is causing me grief. Any suggestions or hints as to how to go about this would be great. Even If I could find the difference between the date and times in hrs or days in a decimal format, I could just multiple by a constant to get my answer. Please note, I do have experience with programming and Excel but please explain in details. I sometimes get lost in steps.
time and date are both stored as numerical, decimal values (floating point actually). Dates are the whole numbers and time is the decimal part (1/24 = 1 hour, 1/24*1/60 is one minute etc...)
Date-time difference is calculated as:
date2-date1
time2-time1
which will give you the answer in days, now multiply by 24 (hours in day) and then by 60 (minutes in hour) and you are there:
time elapsed = ((date2-date1) + (time2-time1)) * 24 * 60
or
C3 = ((A3-A2)+(B3-B2))*24*60
To add a bit more perspective, Excel stores date and times as serial numbers.
Here is a Reference material to read up.
I would suggest you to use the following:
Combine date to it's time and then do the difference. So it will not cause you any issues of next day or anything.
Please refer to the image with calculations. You may leave your total minutes cell as general or number format.
MS EXCEL Article: Calculate the difference between two times
Example as per this article
Neat way to do this is:
=MOD(end-start,1)*24
where start and end are formatted as "09:00" and "17:00"
Midnight shift
If start and end time are on the same day the MOD function does not affect anything. If the end time crosses midnight, and the end is earlier then start (say you start 23PM and finish 1AM, so result is 2 hours), the MOD function flips the sign of the difference.
Note that this formula calculates the difference between two times (actually two dates) as decimal value. If you want to see the result as time, display the result as time (ctrl+shift+2).
https://exceljet.net/formula/time-difference-in-hours-as-decimal-value
get n day between two dates, by using days360 function =days360(dateA,dateB)
find minute with this formula using timeA as reference =(timeB-$timeA+n*"24:00")*1440
voila you get minutes between two time and dates
I think =TEXT(<cellA> - <cellB>; "[h]:mm:ss") is a more concise answer. This way, you can have your column as a datetime.

Converting TEXT that represents NEGATIVE TIME value to a number or time value for adding (Excel)

I've got a spreadsheet (Office 2007 version of Excel) full of text entries that are negative time values, example "-0:07" as in an employee took 7 mins less to complete a job than expected. I need to perform mathematical calculations on these entries and am looking for a more elegant formula/method than I've come up with so far.
I know about 1904 date system and * or / by 24 to convert back and forth, the problem is getting a formula that will recognize the text entry as a negative time value.
I've tried value(), *1, which both work on the text fields if the number is positive, but the "-" seems to mess those up. Even paste-special/add fails to recognize these as numbers.
Here's what I came up with that gets the job done, but it's just so ugly to me:
=IF(LEFT(E5,1)="-",((VALUE(RIGHT(E5,LEN(E5)-1)))*-1.0),VALUE(E5))
Obviously my text entry is in cell E5 in this example.
This works, so I'm not desperate for a solution, but for educational purposes (and smaller code) I'd like to know if there's a better way to this. Does anyone have a suggestion for something shorter, easier?
Thanks.
P.S. - an interesting tidbit here, I use Excel at work, but not at home, so I uploaded a sample spreadsheet to Google Docs, and it actually handles the Value() command on those entries properly. Weird, huh?
Thanks again for any suggestions.
Excel doesn't handle time spans in cells. It only deals with time. When you do "00:07" it is then converted to 0.0048611 which is the same as Jan 1st 1900 12.07 am. So if you did 2 minutes minus 7 minutes it would give at best 11.55pm.
The way you do it is the only way.

Resources