Excel removes leading zero from DATE when using text to columns - excel

I have cells formatted as date with type mm/dd/yy. The dates display correctly while in the date data type. Switching this to text obviously reveals the excel datevalues. using text to columns however does not retain the format specified of mm/dd/yy, and instead always removes the leading zeroes and uses the full year e.g. 1/4/2019. Is there any way to get the text to columns to interpret the format I actually have set?

You can always convert a date to text using the TEXT() function.
For example: =TEXT(A1,"mm/dd/yy") converts the date value in cell A1 to mm/dd/yy format.

Related

Excel auto interpretation of dates as string - switches between US and UK formats unpredictably

Please see image for clearer description of the problem.
When pasting dates into excel it seems to SOMETIMES honor the local system settings for dates (set in Win10 intl.cpl) : in my case UK formatting i.e. DD-MM-YY, but othertimes it uses US formatting i.e. MM-DD-YY. I cannot workout the rules it is following, making it hard to accurately predict how it will interpret large pasted data sets of dates.
As a test, I took the string "02/11/2022 07:00" and pasted into excel. It correctly interpreted this in UK format as a date (in cell B2) as the date: 2-Nov-22 07:00
The source database where I'm copying the date datasets from appends various characters to some dates as markers that unfortunately cannot be removed before copying. So a date might look like: "02/11/2022 07:00*" (note the appended asterix). I need to remove the asterix in excel to make the date usable in excel.
That's where the wierdness comes in:
In the first case (row 3) if I use excel worksheet formulas to trim the asterix with LEFT function, the string remains a string and is not recognised as a date.
In the second case (row 4) if I use vba to trim the asterix with LEFT, the string is recognised in US format (i.e. not honoring local settings) and I get an incorrect date of 11-Feb-22 07:00. So two strange things here:
the excel worksheet LEFT trim result does NOT get recognised as a date, the vba one DOES; and
the vba trim result IS recognised as a date but is interpreted with US settings, even though the local settings are honored in the original 'clean' string case
In the third case (row 5) if I use vba to trim and CDate the string in one line VBA line, excel recognises a date and recognises it correctly in UK format. However if this is done in two steps (trim in one line, paste to cell, and then Cdate the resultant string), excel interferes when the cell gets hold of the string and I get a US date recognised in the middle.
What is most confusing is that if i post a 'clean' date string, excel honors the local interpretation. If I need to manipulate the string to remove artefacts and put a cleaned string in a cell, it doesn't honor local interpretation.
Note: I understand the difference of regional cell FORMATTING which just tells excel how to DISPLAY the date. This question is to do with excel converting a string to a numeric value by sometimes assuming a US date, and othertimes assuming a UK date!
Any definitive advice on how to force excel/vba into using the local date format when it INTERPRETS date strings?
Assuming your pasted strings do not include the visible quotation marks you show
Row 1: Normal behavior
Row 2: The string functions (eg LEFT) return text which will not be auto-converted. To convert to a date value, wrap the results with either the double-unary (--), VALUE function, or DATEVALUE function.
Row 3: VBA is US-centric in converting unqualified date strings
Row 4: CDate respects the windows regional settings.

Comparing dates coming from different files in Excel

I am trying to link two Excel files and compare the values from first one in the second one, but I have a problem at some point.
So the date 28/07/2021 comes from the other file (which is call 3WLA). I had to use the function TEXT(..., "dd/mm/yyyy") otherwise it displays 44405 which is an other form of the date. Now I want to compare this date with other ones but it doesn't work as showed in the pictures below.
The picture shows the comparison of two dates coming from the other excel file 3WLA (still using the function TEXT(..., "dd/mm/yyyy")). We clearly see that there is a problem as it should display TRUE. In the same way, when I compare a date from 3WLA (i.e. using TEXT(..., "dd/mm/yyyy")) with a date random in my file it give me the following
where 28/07/2021 is given by = TEXT('[3WLA.xlsx]Block-First-Fix'!$AM$1,"dd/mm/yyyy") and 29/07/2021 by = 29/07/2021
chris neilsen already gave you the answer in the comments.
To clarify further: The text "29/07/2021" and the date formatted to look like "29/07/2021" are different data types. To help with this MS Excel automatically formats numbers and dates to be aligned to the right and normal text to be aligned to the left
In B1 you can see that the date has been formatted in the same way as per the formula in A2, but since the output in B2 is of the text data type, the two are not considered the same.
You can use the formula DATEVALUE to convert from text to date serial number. (Read more about date serial numbers here).
If your texts with dates are too different from your local settings date formats, DATEVALUE might not give you correct results. In that case use DATE function in combination with some text functions such as LEFT, MID, RIGHT
You can format the cell to make the date more readable using either the number formatting field or the dropdown from the ribbon

Change date format of cell in excel from dd.mm.yyyy to yyy/mm/dd ( excel version 2013 )

I have been searching it for an hour but unfortunately nothing found that actually work. I have an excel sheet with a column having date in it. Current date format is dd.mm.yyyy but I want to change date format to yyyy/mm/dd for entire cell.
OPTION 1)
Assuming that you source date that is in the number format dd.mm.yyyy stored as an excel date serial and only formatted to display as dd.mm.yyyy then the best fix is to select the cells you want to modify. Go to your home tab, and select the number format and change it to General. See Green circles in image below. IF the format is already set to general, or when you switch it to general your numbers do not change, then it is most likely that your date in dd.mm.yyyy format is actually text. and will needed to be converted as per OPTION 2 below. However, if the number does change when you set it to general, select the arrow in the bottom right corner of the number area (see red circle).
After clicking the arrow in the red circle you should see a screen similar to the one below:
Select Custom from the category list on the left, and then in the Type bar enter the format you want which is yyyy/mm/dd.
OPTION 2
=date(Right(A1,4),mid(A1,4,2),left(A1,2))
This assumes your original date is a string stored in A1, and converts the string to a date serial in the form excel stores dates in.1 You can copy this formula down beside you dates. You can then apply cell formatting for the date as described above, or use the build short or long date if that style matches your needs.
1Excel counts the number of days since January 0 1900 for the windows version of excel. I believe mac is 1904 or 1905.
The problem is that your date is a text string, so changing the cell number format won't have any effect.
One method of convert the dates in place to "real" dates is to use the Text to Columns wizard.
Select your date(s).
Select the Text to Columns option on the Data Tools Tab of the Data Ribbon:
At Step 1, select "Fixed Width" then "Next"
Step 2 is irrelevant
At Step 3, for the date column, select DMY as the format. You can also select the "Destination" if you want the results written elsewhere.
Then "Finish".
That's it. Your dates will be converted in place
Since your dates are now "real" Excel dates, you can use the Cell ► Number Format dialog to change the format to whatever you want.
Nothing wrong with your format i.e. yyyy/mm/dd. You can use:
=TEXT(A1,"yyyy/mm/dd")
Update
A comment was made on this question re the TEXT function converting a date-serial to text. It will still be possible to apply date functions to the output of the TEXT function and get an output. E.g. here we can see WEEKDAY returning a result on the value of B1 (which in turn is formatting A1):
You are making this way too complicated. Just use the DATE(year,month,day) function and switch the DAY() and YEAR() inside it.
If in cell A1 you have a European-style date in this format: 12-04-2017; Excel will interpret this as Dec 4, 2017.
Excel will interpret it properly as Apr 12, 2017 if you change it to the American-style of 04-22-2017 by writing this equation in another cell: "=DATE(YEAR(A1),DAY(A1),MONTH(A1))".
(I put the DAY() in the month spot and the MONTH() in the day spot.)
Now you can use any date format you want.
I use 'substitute' to replace the '.' with a '/'.
Then use the date format to get the correct format.

Formula losing date formatting

I have been using the following as a formula array beginning in column R2 for an Excel spreadsheet that combines the data from four columns:
=L2&" "&N2&" "&Q2&" "&P2
Column L contains the date in the following format: 2/23/2015.
The formula used to show the date as shown above, but now for some reason the format has changed to go from 2/23/2015 to 42081.
I can't seem to get date to show as it once did. Also the Excel spreadsheet I apply this to is downloaded from a different source each day so I don't know if default formatting has changed even though the data is exactly the same.
When you concatenate like that you just get the values not the formatting because number formatting doesn't apply in the text string created. You can use TEXT function to dictate the format in this context, e.g.
=TEXT(L2,"m/d/yyyy")&" "&N2&" "&Q2&" "&P2
With MDY convention, if L2 is formatted as Text prior to entry then keying in 2/23/2015 would preserve that display in a formula that concatenates it with other strings. Similarly if not keyed but entered with Ctrl+:.
If L2 is formatted as General or Short Date the date index will be concatenated (42081 in the example above) though the display in L2 will not change.
While a change in formatting may be the most likely explanation (and a solution provided by #barry) another possibility is a change in the date convention from DMY. In which case 2/23/2015 would have displayed and concatenated as that even in a cell formatted as Short Date. 2/12/2015 in that case however would have displayed as such but in the 1900 date system concatenated as 42340 - the date serial number for December 2, not February 12.

Change contents of cell where formatting cells doesn't assist

I have a cell with the data in it in the format of 01/07/2012 6:58:13 AM
However I would like the date only, I have tried to Format Cells and select the format I want 01/07/2012 but the time still remains.
How can i get rid of it?
Assuming that the date for some reasons is stored as text (e.g. you're using a non-English system), you can parse the text and convert it to a proper date with the following function: =DATE(MID(A1,7,4),MID(A1,4,2),LEFT(A1,2))
If you have an English system (it might also work on other locales, just give it a try), you don't have to parse the text, but simply convert it with this formula: =--A1 (and format the cell as Date).

Resources