Convert string to date (1 Viewer)

TimTDP

Registered User.
Local time
Today, 20:28
Joined
Oct 24, 2008
Messages
210
I have the string: 01 April 2016
How do I convert it to a date, in the format: 01/04/2016?

Need to cater for all months of the year!

Thanks
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 03:28
Joined
Jan 20, 2009
Messages
12,854
Assuming your Regional Date Setting is dd/mm/yyyy and language is English.
Code:
CDate("01 April 2019")
 

GohDiamond

"Access- Imagineer that!"
Local time
Today, 13:28
Joined
Nov 1, 2006
Messages
550
If you need a specific output format:
format(cdate("09 april 2016"),"dd/mm/yyyy") = 09/04/2016
format(cdate("09 april 2016"),"mm/dd/yyyy") = 04/09/2016
format(cdate("09 april 2016"),"m/d/yy") = 4/9/16

Cheers!
Goh
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 03:28
Joined
Jan 20, 2009
Messages
12,854
If you need a specific output format:
format(cdate("09 april 2016"),"dd/mm/yyyy") = 09/04/2016
format(cdate("09 april 2016"),"mm/dd/yyyy") = 04/09/2016
format(cdate("09 april 2016"),"m/d/yy") = 4/9/16

Yes, the second in particular is useful when formatting dates for SQL statements (which are strings) where dates need to be in US format.

However it is important to realise that the output of Format() is always a string. If that string is fed to a context where it expects a date datatype it will be implicitly cast to a date using the regional date settings.

Moreover, Windows has an unfortunate "feature" where a date that is invalid in the Regional Date Format will be tested against other valid date formats and, if it matches, it will be quietly accepted.

Here is an example of one of my favorites.
Code:
CDate("29/2/19")
 

Users who are viewing this thread

Top Bottom