Excel formula to compare datetime stamp with today's date - excel

I am trying to compare some datetime stamp with yesterday date, so that we can determine whether the datetime stamp is overdue or within 24h.
Excel formula
And here is the formula that i am using:
=(IF((A2)<(TODAY()-1),"Overdue","Within 24h"))
Apparently, it is always taking "Within 24h" (FALSE) no matter we put any dates. (TEST column)
I tried to truncate the datetime stamp to dd/MM/yyyy but it is still not working.
(TEST2 column).
I tried to use below formula, none of them is working.
=(IF(INT(A2)<(TODAY()-1),"Overdue","Within 24h")) ==> Result "#VALUE!"
=(IF(DATEVALUE(A2)<(TODAY()-1),"Overdue","Within 24h"))
==> Result "#VALUE!"

Looks like I found the culprit.
Apparently, it was due to "Windows Setting => date, time & regional formatting".
Initially, my computer date format was using "mm/dd/yyyy" i.e. "10/27/2021".
And none of these formula were working:
=IF(DATEVALUE(LEFT(A2,SEARCH(":?? ",A2)+2)) < TODAY()-1,"Over","OK")
=(IF(INT(B2)<(TODAY()-1),"Overdue","Within 24h"))
=(IF(DATEVALUE(B2)<(TODAY()-1),"Overdue","Within 24h"))
After I changed the date format into "dd/mm/yyyy" and restart my computer.
Suddenly all of those formula are working since (in my opinion) the excel is able to recognize column A as a text.

I have to say those left-aligned dates in your cells look mighty suspicious. Are you certain they're not formatted as text?
If they are text, then you'll need to convert them to a date. You're close with your last formula, but those seconds/milliseconds (if that's what they are) are causing the DATEVALUE() function to fail.
The easiest thing would probably be to strip out those seconds/milliseconds and do your date compare against the resulting string. Assuming the date as text is in cell A1, the formula would be:
=IF(DATEVALUE(LEFT(A1,SEARCH(":?? ",A1)+2)) < TODAY()-1,"Over","OK")

Related

Change date in excel

I am trying to change the date format of my cells in excel to another date format. Currently it is in this format: apr, 10, 2017 01:58:24 PM. I would like to have it in a normal format like dd-mm-yyyy without the time, but I can not get it to work with the formatting in excel.
Thanks in advance,
Kester
You could use this:
=(MID(A2,FIND(",",A2)+2,FIND(",",SUBSTITUTE(A2,",","#",1))-FIND(",",A2)-2)&"-"&LEFT(A2,FIND(",",A2)-1)&"-"&MID(A2,FIND(",",SUBSTITUTE(A2,",","#",1))+2,LEN(A2)))*1
Which is basically a bit of string manipulation (and some substitution of , to # to help) to put it in the generic format 'd-m-y h:m:s t', which excel understands, then multiply the string by 1 to force into a number (in this case 42835.58222); which you only have to format as date (important!):
Edit: Per comments, the first comma doesn't actually exist, so the revised formula:
=(MID(A2,FIND(" ",A2)+1,FIND(",",A2)-FIND(" ",A2)-1)&"-"&LEFT(A2,FIND(" ",A2)-1)&"-"&MID(A2,FIND(",",A2)+2,LEN(A2)))*1
With data in A1, in B1 enter:
=DATE(MID(A1,10,4),MATCH(LEFT(A1,3),{"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"},0),MID(A1,6,2))
and apply desired formatting:
(this results in a genuine Excel date that can be used in sorts, calculations, etc.)(this assumes that the day field is always two digits)(if your month list is in a language other than English, edit the month list)

Extract date out of cell with date and time

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]"))

Using weeknum on date and time text results with #VALUE

So my friend has this template he got from his boss, it's got a bunch of dates and times and other data. He told me he created a new workbook and copied everything from the template to this new workbook and then he modified the dates. The date and time listing he copied from another file.
So this is where the problem occurs, when he's done copying, one entire column resulted with #VALUE! instead of expected result w/c would be in yy/ww format. The formula is written below:
=MID(N2,9,2)&"/"&IF(LEN(WEEKNUM(N2,1))=1,"0"&(WEEKNUM(N2,1)),(WEEKNUM(N2,1)))
Inside N2 is 26/08/2014 1:27 PM. So I googled a bit and found out that WEEKNUM doesn't work for text-style date and time. Should only work with serial numbers, if I understand it correct. I checked the N column (which was all copied from another file) and they were all in text. But when I checked the template, the dates were also all in text style. So how the formula worked with the template but not with the new workbook? (I'm assuming that every text in every cell is aligned to the left, dates in both new workbook and the template are both aligned to the left.)
I told my friend that the N column should be in date format so I suggested him this formula:
=(DATE(2014,8,26) + TIME(13,27,0))
And finally it worked. But his and my concern would be that we can no longer just copy and paste the dates but manually input every date within the formula. But there's just too many dates and it would take a long time to finish.
Any way around this?
UPDATE:
I just noticed something. The format of the dates copied is dd/mm/yyy h:mm AM/PM. When I checked on the column where all the #VALUE!s are spawning, there are exceptions. I actually found 3-4 cells that are normal.
The date is: 02/10/2014 3:49 PM and the result with using the formula is 15/49. I'm wondering, could it have been with how the dates are typed in? Or rather copied in? Like, should the date format be in mm/dd/yyyy instead of dd/mm/yyyy?
Used evaluate formula and it said that N2 contains a constant.
The WEEKNUM function works with dates as text while it can implicitly convert the text to date. Example:
Input '31/03/2015in A1 and =WEEKNUM(A1) in B1. Input '03/31/2015 in A2 and =WEEKNUM(A2) in B2. Note the leading ' before the dates. They will convert the inputs to text. One of them will work, one will not. This will indicate which is your default date format dd/mm/yyyy or mm/dd/yyyy.
You say both Excel are English but there is a difference in default date format between GB English and US English unfortunately.
So if the dates are text in dd/mm/yyyy format and your default date format is mm/dd/yyyy, you can't simply copy them into your Excel and work with them. You have to change them ever to mm/dd/yyyy first. Ever, because even the values which seems to work are mostly wrong. WEEKNUM with the text 09/11/2015 will work even with default date format mm/dd/yyyy. But it will implicitly convert the text to September 11. 2015 while with dd/mm/yyyy 09. November 2015 was meant.
Best solution ist not to use dates in text format. If your friend's boss had used date values instead of text then copy&paste would work. But because he had not, you have to convert the formats in a helper column now. Try
=MID(N2,4,2)&"/"&LEFT(N2,2)&"/"&MID(N2,7,999)
copied down in a helper column.

Excel #Value error when using DATEVALUE function

In cell A2 I have 7/21/2014 12:44:36 PM
When I use DATEVALUE(LEFT(A2;FIND(" ";A2)-1)) I get the error #VALUE.
When I use LEFT(A2;FIND(" ";A2)-1) I get 7/21/2014.
What do I need do that function DATEVALUE(LEFT(A2;FIND(" ";A2)-1)) to return just the date?
DATEVALUE() is designed to make a Date out of plain text. Your cell is currently a Date/Time, which is a numeric value. I recommend using one of the following solutions to get the date from the cell.
Using DATE()
This is the cleanest option and the method that I would recommend.
=DATE(YEAR(A2),MONTH(A2),DAY(A2))
YEAR() gets the Year value from the cell, MONTH() gets the Month value, and DAY() gets the Day value. The DATE() function takes a Year, Month, and Day value, so by passing them into DATE() we can get the Date value from A2.
Using INT()
If we look at the numeric value of your Date in A2, we see that it is 41841.5309722222. The whole portion of the number (41841.) is the date and the decimal portion (.5309722222) is the time. So if we take INT(A2) to convert this value to an integer, we will lose the decimal portion so that all that remains (41841) is the date. So this is our formula for using INT()
=INT(A2)
The same idea can be accomplished with ROUNDDOWN(A2,0) or =FLOOR.MATH(A2) or =FLOOR(A2,1).
Using DATEVALUE()
While the first solution is the cleanest, there is a way to do this with DATEVALUE() that involves converting the cell into Text first. The TEXT() function takes a value and a format string, so we format the cell value as Text as follows
=TEXT(A2,"yyyy-mm-dd")
And this gives us
2014-07-21
We then pass that result into DATEVALUE()
=DATEVALUE(TEXT(A2,"yyyy-mm-dd"))
You will need to format the result as a date.
Using LEFT() and DATEVALUE()
Based on this Stackoverflow question that I found, it appears the issue could be a result of inconsistent formatting, so you might try this solution
=DATEVALUE(LEFT(A2,FIND(" ",A2)-1))
I have included my results with this and the other methods in a screenshot below. You can see by my use of the TYPE() command below the value that I tested this on both a number and text.
Results
Formatting
I'm assuming you want just the date for calculation purposes, but if you just want to display the date, you can format the cell to only show the date and ignore the time element although the time element will still be present in the cell. I'm assuming you know about this, but since you didn't specify, it is a possible solution.
Please try:
=INT(A2)
and format the result to suit.
In a comment I have just seen you mention "=INT(A2) return #VALUE!" so I would suggest selecting your date cells, DATA > Data Tools, - Text to Columns, Delimited, Delimiters Tab (only), and at Step 3 of 3 choose MDY for Date: or change your locale to say USA.
If neither work try =INT(TRIM(A2)) in case you have leading spaces (though not showing them).
If still not working, try applying =CLEAN.
If still nothing works then some further details of where your dates are coming from and how imported would be helpful, and of your locale and default date format.
Date macros are depending on the system date format and based on configured system date format the macros can fail to work. Try below solution. We faced similar issue after open excel file sent by me on another laptop where system date/time format was different. On destination laptop formulas using date functions started giving error. After following below steps errors disappeared.
Left click on bottom right portion of task bar where time is displayed
Click on date and time settings
Click on change date and time
Change calendar settings
Click on reset to go back to original values
Click on OK on all opened dialogues
Now excel formula error should disappear
SIMPLE SOLUTION - I found a solve that worked very well:
=DATEVALUE(TEXT([CELL],"MM/DD/YYYY")
Effectively this lets me convert any value into text, then back into date. It fixed my Datevalue error and I can use it regardless of the original cell formatting.

How to change the Date Format

I Have 1 Column where there dates are not Uniform.
I am using as a ref to derive one Column.
the date in column is
17/09/2010 i am changing the this format to 09/17/2010 by using =MID(H3,4,3)& MID(H3,1,3)&MID(H3,7,4).
But there are some dates Which are like 9/3/2007 now i want a formula to match all the dates.
Please Help me in this.
I'm assuming that, due to your regional settings, 17/09/2010 isn't recognised as a date and 9/3/2007 is recognised as March 9th, while you want September 3rd.
If you treat your input as text, you can parse it properly into a date with this formula, which I just tested:
=DATE(MID(H3,FIND("/",H3,4)+1,4),
MID(H3,FIND("/",H3)+1,FIND("/",H3,4)-FIND("/",H3)-1),
LEFT(H3,FIND("/",H3)-1))
Make sure H3 is formatted as Text, else the string functions (MID, LEFT) risk not working as expected. The cell containing that formula should be formatted as Date: mm/dd/yyyy.
why not change the cell property of the whole column to User Defined like mm/dd/yyyy

Resources