Convert string to date

TimTDP

Registered User.
Local time
Today, 19:11
Joined
Oct 24, 2008
Messages
213
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:
Assuming your Regional Date Setting is dd/mm/yyyy and language is English.
Code:
CDate("01 April 2019")
 
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
 
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

Back
Top Bottom