Excel - calculate date time - excel

I am trying to calculate the hour difference between two times. What I've been doing now only gives me hour indications like 1:30, 2:45, etc but I can't make diagrams based on these values. If I get 2:30 as the hour difference, it should become 2,5. if the difference is 2:45 that should be 2,75.

Change your formula to:
(B2-A1)*24
and format as General

You should just be able to subtract 1 datetime from the other. Try this:
In cell A1, enter "09/17/2012 10:00" (Excel should automatically recognize this as a date)
In cell A2, enter "09/17/2012 11:30"
In cell A3, "=(A2-A1)*24". The result is 1.5.
The problem may be that you are trying to subtract 1 'time' from another 'time' without a date component. In that case, Excel may not recognize your value as a 'time'. Try adding a dummy date to the beginning of the time.
One limitation of this is that you will get an error response of "########" if the 2nd date is earlier than the 1st (because the result is negative). If this is an issue, try "=ABS(A2-A1)*24" instead.

Related

Extract the hour from the h" hour(s) and "m" minute(s)" in excel

I have the list of data that showing the Hours and the Minutes that I extract from the system. I need to be extract the hours.
As example below, column B first row, the Hours would be 64 and the minutes would be 46.
But when I used the formula =Hour , its turn up the different value since its actually decimal number.
Cannot use left() , it will give the actual decimal number.
Updated:
We tried the #harun24HR 's but cannot readable the value.
But if you noticed, if i copy and paste the value is different. thats why the search not applicable.
4th Update:
To Solar Mike, I have tried the formula given from the thread the i think the value not readable
It's a time value which Excel stores as calculated value based on 24 hours = 1.
To retrieve the hours only you can use:
=INT(A2*24)
To retrieve the minutes only you can use:
=(A1-(INT(A1*24)/24))*24*60
Your time value is already a number in time format so you just need it to change it to decimal system. Dates and time values are numbers. Even if you see it as 30/09/2019 or 12:00:00, actually, for Excel, both cases are numbers.
First date Excel can recognize properly is 01/01/1900 which integer numeric value is 1. Number 2 would be 02/01/1900 and so on. Actually, today is 44659.
Now, about your data, you posted this screenshoot:
So the value is numeric, not text/string. In Excel, what you see is not always what you have. Probably that column has been formatted using custom mask. My fake data is like this:
The numeric value is 02/01/1900 16:46:36 (or 02/01/1900 4:46:36 PM it depends on your regional settings) but I've applied a custom format like this:
[hh]" hours" mm " minutes"
So what I have is a number but what I see is a text!
We have to work with the number (to be honest, ist's easier to work with numbers), so to extract the total hours, minutes and seconds.
Formula in B1: =INT(A1*24) total hours
Formula in C1: =INT(A1*24*60-B1*60) total minutes
Formula in D1: =A1*24*60*60-B1*60*60-C1*60 total seconds
This should guide you on whatever are you trying to achieve.
From your current sample data you try-
For hour =LEFT(A2,SEARCH(" ",A2)-1)
For minutes =RIGHT(SUBSTITUTE(A2," minutes",""),2)

Formatting Existing Excel Cells to Time Format Without Date

I'm working on an excel 2010 sheet where I mark down the date and time an event happens. The date is in one column, and auto formats to 17-Nov when I would type in 11-17 (I was fine with this). The time is in a separate column.
I am trying to find the average time an event occurred, without regard to the date, so I would use =AVERAGE(C1:C10). However, I only receive a date back (like 17-APR).
I did not format the cells before I began to enter in data, and I would simply type in a 3:27pm event as 1527, and no reformatting would happen.
Now, when I attempt to reformat the column to hhmm, all the numbers entered so far turn to 0000. When I try to edit the 0000, it is formatted as 6/13/1906 12:00:00 AM.
What I want to do is have the time formatted as hhmm and not include a date in the cell, and be able to run formulas on it, such as the average time an even occurred.
Summary:
*Currently time is entered simply as ####. I entered 3:27pm as 1527.
*Trying to reformat the time column results in 0000 in all cells in the column that previously had a ####.
*Modifying the 0000 displays as 6/13/1906 12:00:00 AM
*I want to format the time as hhmm so I can simply type in 2357, and have it display as 2357, but understand I mean 11:57pm, and let me take averages.
*Hell, even being able to enter 1547 and have it auto format to 15:47 or 3:47p would be great.
Thanks for reading my question!
An easy way to apply an autoformat (though Excel won't see it as a true "Time") is to go into Format Cells>Custom> and use ##":"##. This will turn 1245 into 12:45. Mind you, this will be a text string so if you copy it to another cell and then apply a time, it will show as 12:00:00. Excel will also not be able to run formulas on it, but it's a quick and dirty way to make it look pretty.
Another option is to have a formula such as =TIME(LEFT(A1,2),RIGHT(A1,2),) where A1 would be replaced with the cell you are actually referencing. This will convert the number to a time that Excel will recognize as a time allowing you to run other functions on it, but requires another column.
If you are entering the times as 4-digit numbers, you'll need to do a calculation to get the hours and minutes, then use the TIME function to get an actual time:-
=TIME(A1/100,MOD(A1,100),0)
Another way is
=LEFT(A1,2)/24+RIGHT(A1,2)/1440
but then you have to format the result as a time.
Excel sees a number like 1547 as approximately 4 years on from 1st January 1900 if you format it as a date, so it will come out as something like 26/3/1904 in UK format or 3/26/1904 in US-style format.
Note that the time function can only give you values up to 23:59:59 (stored as 0.999988426), but the second method will give you a datetime value with one or more days as the whole number part. This can be useful if you want to do calculations on times spanning more than one day.
The above behaviour is because dates and times are stored as real numbers with the whole number part representing days and the decimal part representing fractions of a day (i.e. times). In spite of misleading information from Microsoft here, dates actually start from 31/12/1899 (written as 0/1/1900) with serial number 0 and increment by 1 per day from then on.

Convert date e.g. Jun 05 2016 08:00:00 to dd/mm/yyyy hh:mm:ss

Got input such as in the topic title.
Trying to figure out how to convert this into UK date and time to be used in calculations.
I've looked at some methods on Google such as using text to columns, but I don't think this is what I'm looking for...
Thanks!
Edit: month is always in abbreviated format.
Edit 2: I should mention that I'm in the UK, and it doesn't seem to convert US date automagically.
Edit 3: Data:
Jun 05 2016 08:00:00 to dd/mm/yyyy hh:mm:ss
Assuming that your source date is a string and it is in the cell D10, the first thing you need to do is convert it to an excel date time serial. In the Excel Date Time serial there are a few things to note.
The integer portion of the number represents the number of days since Jan 1 1900 in windows and I think 1904 on mac
The decimal portion of the number represents the time in fraction of a day. 0.5 would represent noon. Valid Excel times for VBA are 00:00 to 23:59:59. 24:00 is not a valid time, though it will work with some excel formulas
So in order to convert your string to an Excel date serial we will need to rip out the components and dump them into the DATE() function. The date function consists of three arguments:
DATE(year,month,day)
Pretty straight forward with the exception that those values need to be numbers. Why dont we start pulling your information going from the largest unit to the smallest unit.
Thankfully your string is of consistent length. You have leading 0 for your single digits so they will occupy the same space as double digits. So this method will work until the year 9999, but I don't think we are too worried about that right now.
In order to pull the year we look at where it is in your string and how long it is. So by simply counting we know it starts in the 8th character position and its 4 characters long. We use this information with the MID() function
=MID(D10,8,4)
In order to pull the month it get a little more complicated since we need to convert it from an abbreviation to a number. There are several ways of doing this. You could go for a long IF statement which would wind up repeating the pull of the month a 11 times. Instead I decided to use the MATCH() function and built an array of month abbreviations inside it. The MATCH() function will return the number/position of what you are searching for in the provided search list. So as long as we enter the months in chronological order, their position will correspond to their numeric values. As such our formula will look like:
MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0)
The LEFT() function was used to pull the month abbreviation from your string. The 0 at the end tell match to look for an exact match. Important to note, this match method is not case sensitive.
Now to get the day we employ the same principals that we did for pulling the year and we wind up with:
=MID(D10,5,2)
We can now substitute each of the formulas for Year Month and Day into the DATE() function and we will get the date portion of the excel date serial. The formula should look like the following:
=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))
Now you need to tack on your time portion or figure out the decimal portion. In order to do this I would first recommend trying the TIMEVALUE() function. Since time formats tend to be a lot more standard in format than dates, there is a much higher probability that it will work for you. In order to use TIMEVALUE(), the time portion needs to be ripped from the string. This can easily be done with the RIGHT() function as follows:
=RIGHT(D10,8)
That will give you just the time portion which can then be substituted into the TIMEVALUE() function and looks like:
=TIMEVALUE(RIGHT(D10,8))
If the TIMEVALUE() function does not work for you, then you will need to strip out the hour minutes and seconds and dump their results in to the TIME() function. Do this in the same way you pulled the year and the day for the DATE() function. Just update your character counts. TIME() uses three arguments as follows:
TIME(HOUR,MINUTES,SECONDS)
Now that you have figured out your date portion and your time portion all you need to do is add them together to get all the information into one cell. The resulting formula will look like:
=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))+TIMEVALUE(RIGHT(D10,8))
Where ever you windup placing that formula, remember to change the formatting on the cell to a custom date. Enter the cell custom format as in the image below.
If you have a list of date times to convert in a column, simply copy your formula and formatted cell down as far as you need to go.
Proof of Concept
Formulas used
For more information on the functions used in the formulas above, follow the links below:
MATCH
DATE
TIMEVALUE
RIGHT
MID
LEFT
{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"}
The { } are used to build a custom list or static array.
If you don't want to write a macro in VBA, a cell formula would work if the format is as you specified:
I am assuming the data is in cell B3
=MID(B3,5,2) & "/" & IF(LEFT(B3,3)="Jan","01",IF(LEFT(B3,3)="Feb","02",IF(LEFT(B3,3)="Mar","03",IF(LEFT(B3,3)="Apr","04",IF(LEFT(B3,3)="May","05",IF(LEFT(B3,3)="Jun","06",IF(LEFT(B3,3)="Jul","07",IF(LEFT(B3,3)="Aug","08",IF(LEFT(B3,3)="Sep","09",IF(LEFT(B3,3)="Oct","10",IF(LEFT(B3,3)="Nov","11","12"))))))))))) & "/" & RIGHT(B3,13)
The DATEVALUE function expects a comma between the day and year; the REPLACE function can add that in. The TIMEVALUE function should be able to read that time without modification.
=DATEVALUE(REPLACE(LEFT(A1, 11), 7, 0, ","))+TIMEVALUE(RIGHT(A1, 8))
Note that the original in A1 is left-aligned; this indicates a text value. The converted date/time in B1 is right-aligned; this indicates a true date/time value.
Column B was formatted as dd/mm/yyyy hh:mm:ss. As General it would show as 42526.33333.

Using this value "2016-05-12 21:51:13 -0500" in Excel

How do I work with a Date Time -0500 value?
I have a sheet that has a value that looks like this:
2016-05-12 21:51:13 -0500
I want to be about to use it.
I want to filter all records that are greater than
2016-05-12 00:00:01 -0500
But I do not know how to work with this value.
Use this formula:
=--LEFT(A1,LEN(A1)-5)
Then format it like this
yyyy-mm-dd hh:mm:ss -\0\5\0\0
Then you can copy and paste the values and formatting where you want it
You need to convert the data into Excel date/times. With data in A1 in B1 enter:
=DATE(LEFT(A1,4),MID(A1,6,2),MID(A1,9,2))+TIME(MID(A1,12,2),MID(A1,15,2),MID(A1,18,2))
and format to display both date and time:
Let's presume for a moment that for some unknown reason Excel could not identify your string as a valid date time. You can always go back to basics and break your string down into its components. Let's start off and assume that your date time and offset string are in cell A1.
Step 1) Strip out the year
=left(A1,4)
Step 2) Strip out the month
=MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1)
That bad boy of a formula looks for the first - and the second - and based on that information finds the starting position to start pulling characters from and figures out how many characters to pull.
In your case we could have set it to pull two characters and had it start at character six as there is no variation to your date format. However, in a generic sense where there are not always leading zeros in the month, or sometimes you were flipping between four characters for the year and two characters for the year, the above would still work.
I am also assuming that month is the middle value (05) and you are not talking about December 5th here.
Step 3) Pull out the day
We could have used a similar approach using mid here, and again we could have hard coded it (wait, I did hard code two character return). Instead for a little flavour I used a right left sequence.
=RIGHT(LEFT(A1,FIND(" ",A1)-1),2)
Step 4) Pull out the time
Now you could go through the whole process and pull out hours, minutes and seconds, but Excel is usually pretty good at recognizing a time format as there is not much variation to it. Also this gives an opportunity to see a new formula for dealing with string manipulation.
Now since your time format was constant, I got a little lazy knowing that your time was always going to be eight characters long since your format always has a leading zero. As such, I used the following:
=TIMEVALUE(MID(A1,FIND(" ",A1)+1,8))
Basically, I grabbed the whole time, HH:mm:ss, and dumped it into timevalue (note there is also a datevalue). Timevalue will attempt to convert a string in time format to Excel time format as a decimal value.
Now as previously noted, if all those times are all stamped with the same -0500, just ignore it.
To get all that date and time converted into a single cell we would take each of the date parts and drop them into the DATE function and then add the time component on. In Excel speak that looks like:
=DATE(LEFT(A1,4),MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1),RIGHT(LEFT(A1,FIND(" ",A1)-1),2))+TIMEVALUE(MID(A1,FIND(" ",A1)+1,8))
Now if you want that to display with the -0500, look at Scott's answer for formatting. If you want to convert the time to local time and get rid of the -0500 then you would need to add -5 hours to the above formula which would look something like:
=DATE(LEFT(A1,4),MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1),RIGHT(LEFT(A1,FIND(" ",A1)-1),2))+TIMEVALUE(MID(A1,FIND(" ",A1)+1,8))+time(-5,0,0)
And if we were not so lazy and did not want to hard code the time, it would look more like:
=DATE(LEFT(A1,4),MID(A1,FIND("-",A1)+1,FIND("-",A1,FIND("-",A1)+1)-FIND("-",A1)-1),RIGHT(LEFT(A1,FIND(" ",A1)-1),2))+TIMEVALUE(MID(A1,FIND(" ",A1)+1,8))+TIME(LEFT(RIGHT(A1,4),2),RIGHT(A1,2),0)*IF(LEFT(RIGHT(A1,5),1)="-",-1,1)

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.

Resources