I received a sheet with thousands of records where the date/time records are incorrect. Some of the time format is missing a leading zero, and I need to trim off the AM/PM and seconds places. Time time needs to be in 24-hour (military) time. Ex:
10/8/2016 80000 AM
Should be: 2016-10-08 08:00 (yyyy-mm-dd hh:mm)
Some of the other records have the correct amount of characters. Ex:
10/8/2016 120000 PM
Should be: 2016-10-08 12:00 (yyyy-mm-dd hh:mm)
Therefore, I can just trim the PM and seconds from the right.
General rule: If the number begins with a 1 it is likely a time between 10:00 and 19:59, if the number begins with a 2 - 9 likely just add a 0 in front and making it 02:00 through 09:59.
My challenge is finding a formula that will accomplish all of this so I don't have to wade through thousands of records to fix the time.
I suspect that you have already figured this out but here is one approach (just because I found it interesting)
So column A holds the actual value
Column C has the following formula to extract the date (presumption here is that first part of the string in column A is a date):
=TEXT(LEFT(A2,FIND(" ", A2, 1)-1), "yyyy-mm-dd")
Column D extracts the time string from column A:
=LEFT(RIGHT(A2,LEN(A2)-SEARCH(" ",A2)),SEARCH(" ", RIGHT(A2,LEN(A2)-SEARCH(" ",A2)))-1)
Now to convert the value in column D to a 24hour time format (without the :) based on AM or PM in column A. So column E is:
=IF(UPPER(RIGHT(TRIM(A2),2))="PM",IF(LEFT(TRIM(D2),1)="1",12+INT(LEFT(D2,2)),12+INT(LEFT(D2,1)))&RIGHT(TRIM(D2),4), D2)
All that is left is to create the required string.. that's where column F comes in:
=C2& " "&IF(LEN(E2)=5,"0"&LEFT(E2,1),LEFT(E2,2))&":"&IF(LEN(E2)=5,MID(E2, 2,2),MID(E2, 3,2))
Not sure if this is the best approach but first one that came to my head :)
Related
I need column C should check the time in column B and compare it with F and G column and display value from column E
Example : 8:30:01 AM is between 4:30 am and 12:30 pm so it should return shift1 as a output in column C
Use:
=IFERROR(INDEX(E:E,MATCH(B2,F:F)),"shift3")
For those with XLOOKUP:
=XLOOKUP(B2,F:F,E:E,"shift3",-1)
I'm having a hard time getting comparisons to work on the time fields. Can you use military time instead and just use the numbers,e.g. 133001 for 1:30:01 pm? If so the following will work
=IF(AND(B3>$H$1,B3<$I$1),$G$1,IF(AND(B3>$H$2,B3<$I$2),$G$2,$G$3))
I have a column with date and time for start (IN) and end (OUT) and I want to calculate the difference in days.
Date: IN Time: IN Date: OUT Time: OUT
24/07/2018 12:15:00 26/07/2018 06:11:00
I combine them into date time IN and OUT by using:
E1=TEXT(A2,"dd/mm/yy ")&TEXT(B2,"hh:mm:ss")
F1=TEXT(C2,"dd/mm/yy ")&TEXT(D2,"hh:mm:ss")
But I am unable to find any working code for the difference in number of hours.
TEXT will convert your dates and times into text (strangely enough).
Adding the date and time will return a date/time value so you could use:
=SUM(C2:D2)-SUM(A2:B2) to return 1.74722222
(edit: if you then give the cell a custom number format of d:h:mm it will display 1:17:56 or 1 day, 17 hours, 56 minutes).
(EDIT AGAIN: Sorry, only just seen you want it in hours. Give the cell a custom number format of [hh]:mm. The square brackets tells it to count over 24 hours. This will return 41:56).
or
=DATEDIF(SUM(A2:B2),SUM(C2:D2),"d") to return 2.
or if you just want days from the dates you could just use
=C2-A2 to return 2.
Dates and times in Excel are just numbers - no need to complicate things by turning them to text.
= (F1 - E1) * 24
Rather easly :)
I have a list of date and time, say 3/5/2018 15:00:00
I want a formula to calculate the time remaining until tonight 00:00
for this example, it should be 09:00:00
this is an image of the resulting table I wanted
what i tried: =TEXT(TIME(24,0,0)-A20, "hh:mm") the result is #Value!
The general algorithm for this, in any programming language or environment is simple:
Get the date component from the date-time value.
Increment the day value.
Subtract the date-time value from the date value in step 2
This works because the value from step 2 is still a date-time value, but the time-of-day component is now zero which represents that day's morning-midnight (i.e. 00:00 in the 24-hour clock).
In Excel, date, time, and date-time values are all represented by decimal numbers where the integer component represents a day-count after some epoch and fractional component represents the time-of-day.
This means step 1 is done by simply doing INT( date-time-value ).
And step 2 is done by simply adding +1 to that value.
And step 3 is simple subtraction, but be sure to set the cell's display format to "Time" and not Date or Date-Time.
Cell B2 should have this formula:
=( INT( A2 ) + 1 ) - A2
Then drag-fill that for the column to get the values for the other rows.
That's it.
So, did this, do note that my answer is in decimals... You can sort to hours, minutes and seconds...
=24-(HOUR(B4)+MINUTE(B4)/60+SECOND(B4)/3600)
This should do:
=24-TEXT(A20,"hh:mm")
Considering midnight is either 0 or 24, depending on where you come from, as we are moving towards it we consider as "24" and when we are moving from it, "0". So I subtract the current time/date in a format that excludes the date from "24" and there you have remaining time to midnight.
Do not forget to format the cell to "time", otherwise the results will be wrong.
I have a sheet with start and end dates and values.
In C I have start date.
In E I have end date.
In I I have start time. (06:05:00)
In J I have end time. (08:33:00)
If C <>E I need to add 24 hours to the time elapsed.
How can I do that?
I tried if(C2<>E2;1+J2-I2;"omitted")
But I get the result 21:32:00.
It should be about 26 hours
(24+8-6 = 26 if we only look at the hours).
What have I done wrong?
Edit;
Back at work and can now upload some images.
Method 1
Method 2
Both return the wrong time.
EDIT2;
Method 3
I remember how I always have to format the dates from "our" format to Excels format for it to be recognized as a date.
In column P I use RIGHT(), MID(), and LEFT() to make a correct formatted date.
In R and S I use the same as P&Q column.
Still not correct.
:-/
EDIT again:
Use formula: =(E1+J1)-(C1+I1) we just add the date and time together and subtract the end from start and then format the cell having the formula to show days, hours, minutes.
This way, if you have more than 1 day difference, you're not just adding 24 hours.
Change the format of the target cell contianing the above formula to
d "days" hh "hours" mm "minutes"
or use the format tigerAvatar suggested of [hh]:mm if you want the hours to be cumulative across days.
Then you get a nice output of: 2 days 02 hours 28 minutes or 50:28
Feel free to drop the 1 h or 1 m if you don't want the leading zeros.
A picture is worth a 1000 words so:
Version 2 after your screenshots: I don't think C, E are in date format based on your updates so...
I used formula=(DATE(LEFT(E2,4),MID(E2,5,2),RIGHT(E2,2))+J2)-(DATE(LEFT(C2,4),MID(C2,5,2),RIGHT(C2,2))+I2) in K and custom format mask: [h]:mm
If this doesn't work it may be a regional setting and the interpretation of [h]:mm I am assuming I/J are time formats.
I have tried every which way to format cells to subtract the result from time for instance the formula in the cell = 11(this is 11 minutes) I want to take that result minus 8:00:00 to give me 7:49:00 but it doesn't work the result is ####### no matter how big I make the cell. And if I format the cells with the formula to custom [m]:ss then the value changes.
Sample of the Worksheet:
I want Y2 = X3-W3 in a time format.
So, if A1=11
Then in some other cell, (B1 in this example): =TIME(,A1,)
Then subtract from the cell with 8:00:00. (If it's C1...:)
=C1-B1
That will give you the time you want.
Info: The main thing is that you have to tell Excel that your cell with the "11" in it, is minutes. By using the =TIME(,A1,) you will get the value of: 12:11 am. (If you keep it in Date format.) 12:11 am could also be viewed as: 0 Hours, 11 minutes, 0 seconds. And now that it knows, you should be able to subtract.
Try this:
=TIME(HOUR(X3),MINUTE(X3)-W3,SECOND(X3))
The ######### is because you have a negative time. Becuase Excel reads time as decimals of 24 hours, 8:00:00 is .3333 and if you subtract 11 from that you get -10.6666 and date/time can not be negative.