Extract date out of cell with date and time - excel

I have a cell G4 with date and time in a format (Text string):
1/29/2020 1:34:24 PM
I need to convert it to DATE formatted cell. How to do that?
I have tried to get numbers and convert them to DATE with this formula:
=DATE((MID(G4;SEARCH("/";G4)+4;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)+2));(MID(G4;SEARCH("/";G4)+1;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)-1));(LEFT(G4;FIND("/";G4;1)-1)))
So:
I am extracting year:
=MID(G4;SEARCH("/";G4)+4;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)+2)
Month
=MID(G4;SEARCH("/";G4)+1;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)-1)
Day
=LEFT(G4;FIND("/";G4;1)-1)
I am getting as a result:
1.5.2022
I need it as it is now, but output should be 29.1.2020 in this case. Later I want to get day difference two that way formatted dates. Is it possible to do it with formula without performing any other cell formatting operations?
EDIT:
I got it working, the only problem is:
How to extract number (year) after third "/"? My current formula is not correct:
=MID(G4;SEARCH("/";G4)+4;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)+2)
It does not function correct in this case:
2/5/2020 12:21:05 PM
EDIT:
I did it this way (I also had to minus G2 - F2, to get days difference):
=IFERROR(DAYS(MID(G2;SEARCH("/";G2)+1;SEARCH("/";G2;SEARCH("/";G2)+1)-SEARCH("/";G2)-1)&"."&LEFT(G2;FIND("/";G2;1)-1)&"."&MID(G2;FIND("/";G2;FIND("/";G2)+1)+1;4);MID(F2;SEARCH("/";F2)+1;SEARCH("/";F2;SEARCH("/";F2)+1)-SEARCH("/";F2)-1)&"."&LEFT(F2;FIND("/";F2;1)-1)&"."&MID(F2;FIND("/";F2;FIND("/";F2)+1)+1;4));"")
You probably need to replace an order of day.month.year and "." to "/" if you are using different date setting (region). I have one setup, so this seems to work.

FYI DATES in excel are stored as integers. They represent the number of days since 1900/01/01 with that date being 1. TIME is stored as a decimal representing fractions of a day or 24 hours. 0.5 represents noon. 24:00 is not an officially supported time in excel, but will work with some functions.
The DATE Formula is looking for three arguments representing YEAR, MONTH, DAY in that order.
DATE(Year, Month, Day)
You need to pull the text from your string representing these values. I find it easiest to pull each one individually in its own cell to ensure the part of the formula is working first then copy and past that part into the DATE formula so the whole calculation in the end can be performed in one cell.
YEAR
To get the year use the following formula:
MID(G4,FIND("/",G4,FIND("/",G4)+1)+1,4)
MONTH
To get the month use the following formula:
LEFT(G4,FIND("/",G4)-1)
DAY
To get the day use the following formula:
MID(G4,FIND("/",G4)+1,FIND("/",G4,FIND("/",G4)+1)-FIND("/",G4)
COMBINED FORMULA
Place the above formulas into the date formula as follows:
=DATE(MID(G4,FIND("/",G4,FIND("/",G4)+1)+1,4),LEFT(G4,FIND("/",G4)-1),MID(G4,FIND("/",G4)+1,FIND("/",G4,FIND("/",G4)+1)-FIND("/",G4)-1))
Note the only cell reference in the formula is G4. The results of the formula are not in an Excel Date format. Change the formatting of your cell to meet your needs. In your case I would apply a custom cell format of d.m.yyyy

If you have TEXTJOIN,
=TEXTJOIN("/",TRUE,INDEX(FILTERXML("<a>,<b>"&SUBSTITUTE(SUBSTITUTE(TEXT(A1,"dd/mm/yyyy hh:mm:ss"),"/","</b><b>")," ","</b>",1)&"</a>","//b"),N(IF({1},{2,1,3}))))
Depending on your version it may need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
the reason the second did not work is that Excel actually changed it to a date and a date is a double, not text. So there are no / in the data. so we need to force back to the incorrect string.

Those for whom the TEXTJOIN function is not available can use this:
=DATE(FILTERXML("<DATA><A>" & SUBSTITUTE(SUBSTITUTE(A1;"/";"</A><A>");" ";"</A><A>") & "</A></DATA>";"/DATA/A[3]");FILTERXML("<DATA><A>" & SUBSTITUTE(SUBSTITUTE(A1;"/";"</A><A>");" ";"</A><A>") & "</A></DATA>";"/DATA/A[1]");FILTERXML("<DATA><A>" & SUBSTITUTE(SUBSTITUTE(A1;"/";"</A><A>");" ";"</A><A>") & "</A></DATA>";"/DATA/A[2]"))

Related

How do I shorten the formula for comparing two dates in excel standard date format?

I have created a formula to compare two dates. M673 is a date that is manually entered but sometime copied over which leaves that unnecessary " "in the front from time to time which makes the date extraction difficult (that's why I trimmed it). The date in N673 is extracted from a manually entered period (i.e. 08/2015) 08 is actually October (10). The extraction formula looks as follows:
(N673)=IF(A673="","",DATE(RIGHT(A673,4),LEFT(A673,2)+3,))
This is to ensure it updates for every period posted automatically.
The below formula says if M673 is before N673 but on the first day of that month and year, then it becomes 1 else its a 0.
Any way to shorten the below formula?
(G673)=IF(DATE(LEFT(TEXT(TRIM(M673),"yyyy-mm-dd"),4),MID(TEXT(TRIM(M673),"yyyy-mm-dd"),6,2),RIGHT(TEXT(TRIM(M673),"yyyy-mm-dd"),2))<(DATE(LEFT(TEXT(N673,"yyyy-mm-dd"),4),MID(TEXT(N673,"yyyy-mm-dd"),6,2),1)),1,0)
Have you tried to use DATEVALUE? The following should work for the scenario you have described above.
(G673)=IF(DATEVALUE(TRIM(M673))<N673,1,0)
There's no need to create a new date based on all the parts. This is what DATEVALUE will do for you automatically.

Convert number to YYYYMM

I have following numbers, which actually represent dates in the YYYYMM format:
201301
201302
201303
...
When I try to format the cells to date fields, Excel always overwrites the original dates and creates some crazy dates, that have nothing in common with the actual ones.
How can I bring Excel to convert it properly?
Excel (by default) uses the 1900 date system. This simply means that the date 1 Jan 1900 has a true numeric value of 1, 2 Jan 1900 has a value of 2 etc. These values are called "serial values" in Excel and it is these serial values that allows us to use dates in calculations. which was discussed here.
So that explains why your data reflects a different date when you try to format is as Date.
What you need to do then is to transform it into a complete readable date format Excel recognize.
As is, Excel will use its default Serial Value conversion.
If you have your data in starting in A1, enter this in B1.
=DATEVALUE(SUBSTITUTE(A1,LEFT(A1,4),LEFT(A1,4)&"-")&"-01")
Above formula will then create a valid Date that Excel can interpret and read correctly.
Result will be:
41275
41306
41334
When you format it as Date, it will equate to the 1st day of every month.
1/1/2013
2/1/2013
3/1/2013
You can just fix the formatting using Custom Format to get YYYYMM as seen below:
If your range is small enough (in the example all 2013) then no formula is required. Replace 13 with 13-, apply Text to Columns with Tab as the delimiter and at Step 3 choose Date and YMD.
You can use the DATE and LEFT/RIGHT functions.
Assuming that format is consistent (i.e. 4 characters for year, 2 characters for month), to convert cell A1:
=DATE(LEFT(A1,4), RIGHT(A1,2), 1)
This will assume the first day of the month for the date (I assume that's irrelevant for you). You can then format the cell as you want.
Can you use formula in Excel to solve this..
A sample formula is given below
=DATE(MID(CELLID,1,4),MID(CELLID,5,2), 1)

date conversion formula in excel

I have some dated in this format "09-MAY-13 06.42.46.097127000 PM" and need to convert to this format "2013-05-09T18:42:47.132Z". my goal is to find out which happend earlier. I am not sure how to format these dates.
2013-05-09T18:42:47.132Z 09-MAY-13 06.42.46.097127000 PM
2013-05-08T20:56:55.821Z 06-MAY-13 03.22.09.129443000 PM
2013-05-08T20:51:45.287Z 06-MAY-13 03.03.22.975700000 PM
2013-05-08T20:55:34.719Z 06-MAY-13 10.40.55.924181000 PM
How I can do that??
I originally posted an answer which converted one of your formats to the other. I now see that you need to compare them, so you can convert this format in A2 to date/number values ....
09-MAY-13 06.42.46.097127000 PM
.....with this formula
=SUBSTITUTE(SUBSTITUTE(A2,".",":",1),".",":",1)+0
and this format in A3
2013-05-09T18:42:46.097z
....with this formula
=SUBSTITUTE(SUBSTITUTE(A3,"T"," "),"z","")+0
You can format the results how you like, leave as numbers or format as m/d/yyyy hh:mm:ss.000 or similar. you can now compare easily with a formula like
=B2>B3
or get the smaller or larger with MIN or MAX
If have a value of your original style in cell A1, place the following formula in a different cell:
=DATE((2000+MID(A1,8,2)),MATCH(MID(A1,4,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),LEFT(A1,2))+TIME(MID(A1,11,2)+IF(RIGHT(A1,2)="PM",12,0),MID(A1,14,2),MID(A1,17,12))
Then, in that target cell, apply the following format:
yyyy-mm-ddThh:mm:ssZ
Please note the following:
This assumes that all of your months are 3 characters (JAN, FEB, MAR). Since your example was "MAY" (already 3 characters), I don't know with certainty how your other months appear. Longer month names complicate the formula a bit. I can help with this if you need it and don't know how to do it.
I couldn't find a way to force Excel to display fractional seconds in the context of such complex date/time formatting, even though your example showed seconds subdivided to the thousandths. Hopefully this isn't a deal-breaker. If it's necessary to display fractional seconds, you may need to handle this with a separate formula to display the result as text, independent of the formula above, which you can use for your calculations of which date came earlier.
To add to Joe's answer...
To get Excel to compare one column to the other you need to do the same sort of manipulation from the first column as the second.
That would look like
=DATE(LEFT(A1,4),MID(A1,6,2),MID(A1,9,2))+TIME(MID(A1,12,2),MID(A1,15,2),MID(A1,18,5))
Once you have a new column for each of these transformations it should be simple to have Excel compare them.

Can I use the CountIf function to count the # of cells containing certain date keywords?

I have a set of data that looks something like this:
11/8/12 5:20
11/7/12 15:57
11/7/12 13:51
11/7/12 10:47
11/7/12 8:00
Just in Excel, I want to be able to count the number of rows with 11/8/12, and the number of rows beginning in 11/7, etc. I have a pretty long list of dates and times, but I'm only interested in the dates.
I tried =COUNTIF(A1:A235, "<11/8/12 5:20") to get everything before 11/8, but obviously this doesn't work because I need to type in the time. Does anyone know how to:
1) make the timestamp irrelevant?
2) somehow search for rows that "contain" a certain date value and count up the rows that contain that value?
You can insert in a cell e.g. F7 the date which should be used
and then your formula will look like this
=COUNTIF(E8:E12;"<"&F7)
And then in case that your F7 will contain 11/7/12 14:00 the countif will return 3
NOTE: expecting the british date 11/7/12 is dd/mm/yy
EDIT: Date function
When I've seen another answer, I would like to show another approach, which is culture safe. Whenever we will put some strings representing dates, it could happen that a collague has different regional settings (e.g. British dd/mm/yy vs US mm/dd/yy)
That's why I placed the date into separated cell, representing the Date regardless of the culture. If that all have to be placed inside one cell, the correct approach is to use Date function
=COUNTIF(E8:E12;"<"&DATE(2012;7;11)+TIME(14;0;0))
In Excel dates are integers and the time is a fractional value so you can simply count between one date and the next (date +1) to get everything on a single date, e.g. for everything on 11/7
=COUNTIF(A1:A235, ">=11/7/12")-COUNTIF(A1:A235, ">="&"11/7/12"+1)
or in Excel 2007 or later you can use COUNTIFS
=COUNTIFS(A1:A235, ">=11/7/12",A1:A235,"<"&"11/7/12"+1)
for either one you can use a cell reference with the date as Radim suggests, i.e. with date in B1
=COUNTIFS(A$1:A$235,">="&B1,A$1:A$235,"<"&B1+1)
With that last version you can simply list all your dates in B1 down, and then use the formula in C1 copied down to get counts for each date

Excel Calculate the date difference from today from a cell of "7/6/2012 10:26:42"

So I have a cell with 7/6/2012 10:26:42 inputted, I want to show the date difference from today in another cell.
I tried to extract 7/6/2012 with =LEFT(A1, Find(" ", A1, 1) -1) but turned out theres a value error.
The formula works when I make A1 '7/6/2012 10:26:42, however it is not ideal because I have to work with the whole column.
You can use the datedif function to find out difference in days.
=DATEDIF(A1,TODAY(),"d")
Quote from excel.datedif.com
The mysterious datedif function in Microsoft Excel
The Datedif function is used to calculate interval between two dates in days, months or years.
This function is available in all versions of Excel but is not documented. It is not even listed in the "Insert Function" dialog box.
Hence it must be typed manually in the formula box.
Syntax
DATEDIF( start_date, end_date, interval_unit )
start_date from date
end_date to date (must be after start_date)
interval_unit Unit to be used for output interval
Values for interval_unit
interval_unit Description
D Number of days
M Number of complete months
Y Number of complete years
YD Number of days excluding years
MD Number of days excluding months and years
YM Number of months excluding years
Errors
Error Description
#NUM! The end_date is later than (greater than) the start_date
or interval_unit has an invalid value.
#VALUE! end_date or start_date is invalid.
If that's a valid date/time entry then excel simply stores it as a number (days are integers and the time is the decimal part) so you can do a simple subtraction.
I'm not sure if 7/6 is 7th June or 6th July, assuming the latter then it's a future date so you can get the difference in days with
=INT(A1-TODAY())
Make sure you format result cell as general or number (not date)
For the difference between A1 and Today's date you could enter:
=ABS(TODAY()-A1)
which returns the (fractional) number of days between the dates.
You're likely getting a #VALUE! error in your formula because Excel treats dates as numbers.
DAYS(start_date,end_date):
For example:
DAYS(A1,TODAY())
Why don't you just make it easy and simple. If I need to know the number of days between today and say, March 10th, 2015, I can just enter the simple formula.
Lets say the static date is March 10th, 2015, and is in cell O5.
The formula to determine the number of days between today and O5 would be, =O5-Today()
Nothing fancy or DATEDIF stuff. Obviously, the cell where you type this formula in must have a data type of 'number'. Just type your date in normally in the reference cell, in this case O5.
=ROUND((TODAY()-A1)/365,0) will provide number of years between date in cell A1 and today's date
*In all instances the # refers to the cell number
You really don't need the datedif functions; for example:
I'm working on a spreadsheet that tracks benefit eligibility for employees.
I have their hire dates in the "A" column and in column B is =(TODAY()-A#)
And you just format the cell to display a general number instead of date.
It also works very easily the other way: I also converted that number into showing when the actual date is that they get their benefits instead of how many days are left, and that is simply
=(90-B#)+TODAY()
Just make sure you're formatting cells as general numbers or dates accordingly.
Hope this helps.

Resources