|
-
Jul 10th, 2007, 05:15 AM
#1
Thread Starter
Frenzied Member
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"
-
Jul 10th, 2007, 05:26 AM
#2
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.
-
Jul 10th, 2007, 06:21 AM
#3
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.
-
Jul 10th, 2007, 06:36 AM
#4
Thread Starter
Frenzied Member
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.
-
Jul 10th, 2007, 06:47 AM
#5
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.
-
Jul 10th, 2007, 07:55 AM
#6
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.
-
Jul 10th, 2007, 08:49 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|