+ Reply to Thread
Results 1 to 8 of 8

Date format not working as expected when extract from text string

  1. #1
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    232

    Question Date format not working as expected when extract from text string

    Good morning,

    I have some date data supplied as a text string (in column A of attached).

    In cell A2; 2016-10-13T11:20:16.000

    I have written a formula to extract the date & time, combining them;

    =TEXT(LOWER(LEFT(SOURCE!A2,10)&" "&MID(SOURCE!A2,12,5)),"dd/mm/yyyy hh:mm")

    BUT try as I might I cannot get excel to 'see' this as a date or time - for example, it doesn't convert it to a number '4XXXX' when saved as a csv.

    Columns C and D are an attempt to see if it IS seeing it as a date format of some kind.

    What can I do to make sure it's exporting as a date in the column B formula please?

    Thanks,

    Ian
    Attached Files Attached Files

  2. #2
    Forum Expert avk's Avatar
    Join Date
    07-12-2007
    Location
    India
    MS-Off Ver
    Microsoft Office 2013
    Posts
    3,223

    Re: Date format not working as expected when extract from text string

    This is a format issue.
    Kindly change format in custom as :
    PHP Code: 
    _($* #,##0_);_($* (#,##0);_($* "-"_);_(@_) 


    atul


    If my answer (or that of other members) has helped you, please say "Thanks" by clicking the Add Reputation button at the foot of one of their posts.

    Also: if your problem is solved, please take the time to mark your thread as SOLVED by going to the top of your first post, selecting "Thread Tools" and then "Mark thread as solved".

  3. #3
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Date format not working as expected when extract from text string

    Select B2, Format as short date not a text, edit B2, enter, drag formula down, export to csv

    Edit:
    I forgot about next columns: change in C2: =--B2 and in D2: =--B2
    Attached Files Attached Files
    Last edited by sandy666; 01-17-2017 at 07:41 AM.

  4. #4
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Date format not working as expected when extract from text string

    Quote Originally Posted by iantix View Post
    In cell A2; 2016-10-13T11:20:16.000

    I have written a formula to extract the date & time, combining them;

    =TEXT(LOWER(LEFT(SOURCE!A2,10)&" "&MID(SOURCE!A2,12,5)),"dd/mm/yyyy hh:mm")
    I didn't download your file.

    This worked for me...

    =--TEXT(LEFT(SOURCE!A2,10)&" "&MID(SOURCE!A2,12,5),"yyyy-mm-dd hh:mm")

    Then, format in the date/time style that you desire.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  5. #5
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    232

    Re: Date format not working as expected when extract from text string

    Thank you all - Tony, I went with yours as I was keen to get a formula that would work, rather than focus on cell format alone.

    Curious to know what the difference between;

    =TEXT(LOWER(LEFT(SOURCE!A2,10)&" "&MID(SOURCE!A2,12,5)),"dd/mm/yyyy hh:mm")

    and

    =--TEXT(LEFT(SOURCE!A2,10)&" "&MID(SOURCE!A2,12,5),"yyyy-mm-dd hh:mm")

    Is - the -- at the start, what does that actually do to change things up, please?

    (pretty sure the 'lower' in mine was superfluous also)

    Interesting your formula has year/month/day in reverse to mine, yet displays as 11/05/2016 (for example) on mine.

    Ian

  6. #6
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Date format not working as expected when extract from text string

    Quote Originally Posted by iantix View Post

    Is - the -- at the start, what does that actually do to change things up, please?
    The TEXT( ) function returns a TEXT value while a true date/time value is a numeric value.

    For example, the true date value 1/17/2017 is really just a number formatted to look like a date.

    In Excel dates are really just numbers formatted to look like dates. These numbers are commonly referred to as the date serial number. The date serial number is the count of days starting from a base date. In Windows Excel the default base date is January 1, 1900. So, January 1, 1900 is date serial number 1.

    Jan 1 1900 = date serial number 1
    Jan 2 1900 = date serial number 2
    Jan 3 1900 = date serial number 3
    Jan 4 1900 = date serial number 4
    Jan 5 1900 = date serial number 5
    etc
    etc
    Jan 1 1975 = date serial number 27395
    Jan 1 2000 = date serial number 36526
    Jan 1 2017 = date serial number 42736

    You can see the date serial number by entering some date in a cell and then changing that cells format to General or Number.

    So, when we use the TEXT( ) function:

    TEXT(LEFT(SOURCE!A2,10)&" "&MID(SOURCE!A2,12,5),"yyyy-mm-dd hh:mm")

    This returns a TEXT string that looks like a date/time: 2016-10-13 11:20

    So we need to convert that TEXT value into a true numeric date/time value. One way to do that is to use the double unary minus --.

    --TEXT(LEFT(SOURCE!A2,10)&" "&MID(SOURCE!A2,12,5),"yyyy-mm-dd hh:mm")

    Now the result is the number 42656.47222

    Then we can format that number to display in whatever date/time we desire.

    (pretty sure the 'lower' in mine was superfluous also)
    Yes, it was not needed.

    Interesting your formula has year/month/day in reverse to mine, yet displays as 11/05/2016 (for example) on mine.
    Yeah, the date formats from country to country vary but the yyyy-mm-dd seems to be a fairly "standard" format recognized internationally.

  7. #7
    Forum Contributor
    Join Date
    08-21-2014
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    232

    Re: Date format not working as expected when extract from text string

    Tony - LOVE your work - thanks for taking the time to clarify that and enhance my understanding of excel sorcery

  8. #8
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Date format not working as expected when extract from text string

    You're welcome. Thanks for the feedback!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. extract a date from a text string
    By theobrandt in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-02-2016, 08:30 AM
  2. Excel Extract Year from Text String Date
    By Ankit_Kumar in forum Excel General
    Replies: 2
    Last Post: 07-15-2015, 12:16 AM
  3. [SOLVED] FORMAT not working for Date to text as expected
    By JimSnyder in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2013, 05:30 PM
  4. String not working as expected
    By WilyOne in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-21-2011, 08:44 AM
  5. Excel VBA-Date format "mmm" not working as expected
    By sbishops in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-23-2009, 06:40 PM
  6. Excel VBA-Date format "mmm" not working as expected
    By sbishops in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-23-2009, 06:39 PM
  7. Date format "mmm" not working as expected
    By sbishops in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-23-2009, 06:32 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1