Results 1 to 7 of 7

Thread: Convert DateTime to Date

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Convert DateTime to Date

    I have data that has been returned from an SQL query that looks like this
    2005-07-13 00:00:00.000
    Unfortunately it is not recognised as a date so this doesn't work
    Code:
    Format("2005-07-13 00:00:00.000", "dd/mmm/yyyy")
    Is there any short (1 line) way of converting it to 13/Jul/2005?

    I can create my own function and use split, but I was wondering if there was a built in way.
    Interestingly Excel has no problem formatting it with "dd/mmm/yyyy"

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Convert DateTime to Date

    The milliseconsd can't be handled. Removing it will use PC regional settings for parsing the date in the string. Still, it would be best to manually parse the string (your gonna modify the string anyway to remove the millisecond info) if you know its format and create the date using DateSerial() so you can avoid issues with regional settings.
    Last edited by leinad31; Jul 10th, 2007 at 05:31 AM.

  3. #3
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Convert DateTime to Date

    Yes you can use Format

    MsgBox Format(Now, "dd/mmm/yyyy")

    Well it works for Now.

    Now is a Date and Time.
    Last edited by Keithuk; Jul 10th, 2007 at 06:25 AM.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  4. #4

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Re: Convert DateTime to Date

    Thanks Keith, but the point is that "2005-07-13 00:00:00.000" is not the same as Now and it isn't a Date.

    I think I'll follow what leinad31 said and use DateSerial.

  5. #5
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Convert DateTime to Date

    Yes but the reason your doesn't work is

    Format("2005-07-13 00:00:00.000", "dd/mmm/yyyy")

    Incorrect time value

    Format("2005-07-13 13:05", "dd/mmm/yyyy")

    This works.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Convert DateTime to Date

    It will work now, but it may not work later.. it'll be vulnerable to user changes to short/long date format.

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: Convert DateTime to Date

    Just remove the milliseconds, and then you will have a valid date format:

    Format(Split("2005-07-13 00:00:00.000", ".")(0), "dd/mmm/yyyy")

    And since you don't use the time, you might as well, remove everything from the right of the space...

    Format(Split("2005-07-13 00:00:00.000", " ")(0), "dd/mmm/yyyy")

Posting Permissions

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



Click Here to Expand Forum to Full Width