Excel: Text String to Date Conversion - excel

I have a working formula for the following text to date conversion. However, I don't understand why the trailing 0 has to be added in order to show the year in YYYY format.
21.04.2016 converts to 4/21/2016
=(MID(A2,4,2)&"/"&LEFT(A2,2)&"/"&RIGHT(A2,2))+0
It's very simple and straight-forward. However if you remove the 0 at the end of the formula, it will only show 16 instead of 2016. But I could do Right(A2,4) instead of Right(A2,2). But I still like to know why? Anyone? Thanks!

The trailing zero turns the whole thing into a math operation which causes the string (everything to the left of the +0) to be treated as a number.
you could also use *1 instead of +0
=(MID(A2,4,2)&"/"&LEFT(A2,2)&"/"&RIGHT(A2,2))*1
or you could drop the +0 and at the front add -- before the ( and it should all do the same.
=--(MID(A2,4,2)&"/"&LEFT(A2,2)&"/"&RIGHT(A2,2))

As Ed has correctly answered, this is because it is treating your date string as a number.
However, Excel has to interpret the string to get what number it really is, and to do this it relies on regional settings.
Under US regional settings, your formula works great, but when I plug it into excel with UK regional settings I get #Value because "04/21/16" isn't a valid date or number in the UK.
In order to avoid this problem, you should convert it to a date using the DATE() function, which will work irrespective of your regional settings.
=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))

Related

Locale-independent Text function in Excel

I need to format dates in excel, and I'm trying to use the TEXT formula. The problem is that Excel's intepretation of the arguments changes when the locale changes.
For example: if I have a date in cell A1, that i'd like to convert to text, in the year-month-day-format, I have to use =TEXT(A1, "yyyy-mm-dd") if my PC has an English-language locale, but =TEXT(A1, "jjjj-MM-tt") (I kid you not, the M has to be upper case) if it has a German-language locale. This makes the document unportable. (The second argument is plain text and therefore not converted when changing locale.)
Remarks:
This is just an example, I know I could do the long =YEAR(A1) & "-" & TEXT(MONTH(A1), "00") & "-" & TEXT(DAY(A1), "00") in this case. I'm wondering about the more general case.
The date should not just be displayed in a certain format, it should actually be a string. For someone viewing the file this doesn't make a difference, but when using it in other formulas, it does.
I could write a UDF in VBA to solve the issue, but I cannot use VBA in this document.
I do not care about changing the names of the months etc. It's fine, if the name of the month is June or Juni depending on the locale.
I want to stress that the issue occurs due to the PC's locale - not due to the GUI language of the MS Office version. In the example above, Excel's GUI and formulas were in English in both examples; I just changed the locale on the machine.
Many thanks
Here is a slightly cheaty method: Use a VLOOKUP on a value that will change based on your System Language - for example TEXT(1,"MMMM")
=VLOOKUP(TEXT(1,"MMMM"),{"January","yyyy-MM-dd";"Januar","jjjj-MM-tt"},2,FALSE)
In English: Text(1,"MMMM") = "January", so we do a VLOOKUP on the Array below to get "yyyy-MM-dd"
"January" , "yyyy-MM-dd" ;
"Januar" , "jjjj-MM-tt"
Auf Deutsche, Text(1,"MMMM") = "Januar", also wir machen einen SVERWEIS auf dem Array oben, um "jjjj-MM-tt" zu erhalten! :)
Then, just use that in your TEXT function:
=TEXT(A1, VLOOKUP(TEXT(1,"MMMM"),{"January","yyyy-MM-dd";"Januar","jjjj-MM-tt"},2,FALSE))
Obviously, the main reason this works is that TEXT(1,"MMMM") is valid for both German and English. If you are using something like Filipino (where "Month" is "Buwan") then you might find some issues finding a mutually intelligible formatting input.
I found another possibility. It is not perfect in all cases (see below) but it also works with number formats to be locale independent. As I have the same issue with mixed language versions.
For this you make your own function in vba. Open the developer tools with Alt+F11 and create a new module file. Inside the module file paste something like this:
Function FormatString(inputData, formatingString As String) As String
FormatString = Format(inputData, formatingString)
End Function
Then you can use it in cell formulas with english formating strings. Like:
= FormatString(A1; "yyyy-mm-dd")
Advantage: It also works with number formats:
= FormatString(A1; "00.00")
In case (like Germany) your decimal separator is not a .
Drawbacks:
1 Not identical to TEXT function
this doesn't always work with date formatting as maybe expected and not exactly the same as the TEXT function:
FormatString(1; "MMMM")
does not return "January" but "December" because the 1 is taken as a date. Which is something like 31.12.1899.
2 Has to be saved with macros
You have to save the file as *.xlsm for this to work
Note (1): this answers only the case for locale-independent TEXT to format numbers with decimal symbols and digit grouping symbols. For date formatting, see Chronocidal's answer.
Note (2): this answer does not use VBA functions, which would require enabling macros. Enabling macros may not be possible depending on the company's security policy. If enabling macros is an option, Uwe Hafner's answer would be easier.
You can detect the decimal symbol and digit grouping symbol as follows. Enter the number 1 in a specific cell (e.g. A1) and the number 1000 in another cell (e.g. A2).
Decimal symbol: =IF(TEXT(INDIRECT("A1"),"0,00")="001",".",",")
Digit grouping symbol1: =IF(TEXT(INDIRECT("A2"),"#,###")="1000,",".",",")
This is assuming that the decimal symbol is either . or , and the digit grouping symbol is either , or . respectively. This will not detect unusual digit grouping symbols like (space) or ' (apostrophe).
With this information, you can set up a cell (or cells) with a formula that results in the format code you need to apply.
Suppose you need to format a number to two decimal digits and using the digit grouping symbol. You can assume that if the decimal symbol is . then the digit grouping symbol will be , and vice versa. You can do the following:
A1: 1
A2 (the formatting string): =IF(TEXT(INDIRECT("A1"),"0,00")="001","#,##0.00","#.##0,00")
A3 (contains an arbitrary number you wish to format)
A4 (the formatted number): =TEXT(A3,A2)
Technical note: the INDIRECT function is used intentionally because it is a volatile function. This guarantees that the formatting string and anything dependent on it is recalculated even if no data changed in the Excel document. If INDIRECT is not used, Excel caches results and will not recalculate the formatting string when the Excel document is opened on a PC with different locale settings.
1 - Also known as Thousands separator
The easy fix, whether directly custom formatting a cell or using TEXT(), is to use a country code for a language you know the proper formatting codes for.
For instance, I am in the US, have a US version of Excel, and am familiar with its date code formats. So I'd want to use them and to ensure they "come out" regardless of anyone's Windows or Excel version, or the country they are in, I'd do it like the following (for TEXT(), let's say, but it'd be the same idea in custom formatting):
=TEXT(A1,[$-en-US]"yyyy-mm-dd")
The function would collect the value in A1, ask Excel to treat it as a date, Excel would and would say fine, it's cool (i.e.: the value is, say, 43857 and not "horse") because it is a positive number which is a requirement for anything to be treated as a date, and let the function move on to rendering it as a date in the manner prescribed. Rather than giving an #ERROR! as it would for "horse" or -6.
The function would then read the formatting string and see the language code. It would then drop the usual set of formatting codes it loaded upon starting up and load in the formatting codes for English ("en") and in particular, US English ("US"). The rest of the string uses codes from that set so it would interpret them properly and send an appropriate string back to TEXT() for it to display in the cell (and pass on to other formulas if such exist).
I have no way to test the following, but I assume that if one were to use a format that displayed day of the week names or month names, they would be from the same language set. In other words, Excel would not think that even though you specified a country and language that you still wanted, say, Dutch or Congolese month names. So that kind of thing would still need addressed, but would be an easy fix too just involving, say, a simple lookup one could add though it'd be "fun" setting up the lookup table for each language one wanted to accomodate...
However, the basic issue that arises with this problem in general, is very, very easily solved with the country codes. They aren't even hard or arcane anymore now that the [$-409] syntax has been replaced with things like [$-en-us] and [$-he-IL] and so on.

Excel bug: TEXT function doesn't work to extract day name depending on region

How to compute day name from date in Excel?
Please don't say it is TEXT(...,"ddd") because it doesn't work
Another screenshots for non-believers:
Complete formula just doesn't work too:
This is some problem with locale processing. Although my Windows in English, my region is Russia and Excel uses it in some strange places:
TEXT(...,"ddd") ( or TEXT(...;"ddd") . as required )
does work, provided you either SUBSTITUTE the dots . in your data for recognisable date separators first (eg /) or apply Find/Replace for that purpose. Though having done either (perhaps working on a copy) no formula is necessary since merely a Custom format of:
dddd
(long form, or ddd short) should be sufficient.
Note that without indication of the century Excel will guess which and not give you the right answer for a date such as 11.11.1911 (Armistice Day, a Saturday) represented in text as 11.11.11.
With string parsing you would need to be careful whether 10.08 represents October 8 on your system, or August 10.
We can always manually build one : =if(WEEKDAY(A1,2)=7,"Sunday",if(WEEKDAY(A1,2)=6,"Saturday",if(WEEKDAY(A1,2)=5,"Friday",if(WEEKDAY(A1,2)=4,"Thursday",if(WEEKDAY(A1,2)=3,"Wednesday",if(WEEKDAY(A1,2)=2,"Tuesday",if(WEEKDAY(A1,2)=1,"Monday","")))))))
[^_^]

Can't convert Date to Number

I have a userform and I take start and end dates from user as in dd.mm.yyyy format. To make it easier to compare dates, I want to turn it to a double or long type of value. I have tried as in below but it gives me an error of type mismatch.
endDate is already defined as Double and as you can see, Me.g_end.Value is string in proper format. Why do I get this error, and how can I handle it?
Also I need to add, DateValue(Me.g_end.Value) works fine with my friend to get value of date who uses Excel 2013. I use Excel 2016.
You need to enter dates in a valid format in order for Excel to recognize them as dates.
As far as I know, nowhere uses dots (periods) in dates. (As I recall, nowadays only one country in the world even recognizes dots as am official date separator.)
Click your Start menu and type Region to find and open Windows Region and Language Settings.
Note the format that your system is expecting for Short Date, including the symbol between each date part, and try entering your dates in Excel like that.

Excel not recognising a number

for some reason my excel drops error when multyipling on if statemtn
for example
=IF(A1>B1;A1*5;A1)
I cant do nothing, if Id skip it as 5x5 for exmaple, than it works perfect, though If I use an cell for a multiply, it drops me Value error.
I examined it and calculation steps are:
=IF(TRUE;"20.00"*5;A2), the 20.00 is what is A2 cell.
=IF(TRUE;#VALUE!;A2)
So I am stuck and broken...
This is strange. When multiplying a string that looks like a number, Excel normally automatically converts the string to a number. Try this to force conversion:
=IF(A1>B1;VALUE(A1)*5;A1)
If that still results in an error, then Excel can't recognize "20.00" as a number. Does your locality use a comma instead of a fullstop for the decimal places? If so, try putting 20,00 in A1.
Also, try checking your locality settings. From the Start menu, search for Locality or Language, then choose to change your Date, Time or Number Formats. Click Additional settings and check what your Decimal Symbol is. You need to use this decimal symbol in Excel for it to correctly recognize numbers.
Please use this formula,
=IF(A1>B1,PRODUCT(A1,5),A1)

Problems changing date format of date as string

I am currently trying to convert yyyymmdd type of date to a ddmmyyy format.
I've tried using DATE function and something like this:
=DATE(LEFT(A3;4);MID(A3;5;3);RIGHT(A3;7))
Original date for this function is 20120401 but the formula returns: 16.12.2104.
I've tried using functions YEAR, MONTH and DAY instead of LEFT, MID and RIGHT but that's not the right way.
I also tried using DATEVALUE but since Excel probably doesn't recognize yyyymmdd as a date, it gives me a #VALUE! error.
I've found a couple of solutions for SQL but it isn't my strong side yet - and this should be achievable easily in Excel.
Is there a way to do this in Excel?
Applying =DATE(LEFT(A3;4);MID(A3;5;3);RIGHT(A3;7)) to 20120401 in A3 is effectively to reassemble the component characters as below on the left:
These effectively become converted as on the right.
The solution was simply to exclude the digits highlighted red with:
=DATE(LEFT(A3;4);MID(A3;5;2);RIGHT(A3;2))
but the locale might have been a factor and when dealing with date serial numbers and/or what appears to be a date but is a string various other issues might have been involved.
Consider:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))

Resources