4

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!

6
  • 1
    Wondering how were you able to find it out that adding the zero would work !! ;)
    – SrinR
    Apr 27, 2016 at 11:05
  • 1
    Truth is, this code is not from me. I saw it somewhere online. I just don't understand the reason behind it.
    – purpleblau
    Apr 27, 2016 at 11:07
  • 3
    Adding a zero makes Excel convert a string into a number, proper formatting makes Excel display the number as a Date. Apr 27, 2016 at 11:08
  • 1
    @Gary'sStudent You are too fast! Go reward yourself with a trip to the local coffee/donut shop!
    – Forward Ed
    Apr 27, 2016 at 11:11
  • 2
    @lulumink only in some cases. Apr 27, 2016 at 11:40

2 Answers 2

7

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))
1
  • Now, it becomes clear! Thanks alot! If we add a math operator to the formula, Excel treats it like a number instead of anything else. Makes sense!
    – purpleblau
    Apr 27, 2016 at 11:26
6

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))
7
  • Thanks Phil for the alternative solution! Works like a charm too!.
    – purpleblau
    Apr 27, 2016 at 11:28
  • 1
    This is the better answer, avoiding relying on local settings to dis-ambiguate the date Apr 27, 2016 at 11:33
  • 1
    Agreed that this is the better way to convert the date and totally support this as the answer (read upvote) to get the date from a string into an excel date value...but technically the question was essentially "what the heck is the +0 doing?" But that might be splitting hairs.
    – Forward Ed
    Apr 27, 2016 at 11:40
  • 1
    Another advantage of using the DATE()function is that the cell will automatically a reasonable format Apr 27, 2016 at 11:43
  • 1
    @ForwardEd I completely agree that this wasn't really the question, but the title of the question was screaming out for this clarification and I didn't have comment priviledges
    – Phil
    Apr 27, 2016 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.