AlexVB
AlexVB

Reputation: 257

VBA: reading date from a string of a specific format

I'm looking for a way to convert strings to Date data type in VBA. I can use CDate() function for this purpose, but it uses its own implicit algorithm for parsing strings and I would like to specify my own format to differentiate between "DD/MM/YY" and "MM/DD/YY", for example.

Please notice that unlike Visual Basic, VBA has no DateTime.TryParseExact function.

ADD: As some answers suggest, I can write my own parser. If there is no built-in solution, it will be my choice.

Upvotes: 1

Views: 3651

Answers (3)

Achim Gmeiner
Achim Gmeiner

Reputation: 151

In this case I would avoid CDate alltogether and instead split the date string into year, month and day and use DateSerial(year, month, day). This way you have full control.

Upvotes: 1

ShanayL
ShanayL

Reputation: 1247

Do something like this...

strDate = Split(strDate,"/")
newstrDate = strDate(1) & "/" & strDate(0) & "/" & strDate(2)
strDate = CDate(newstrDate)

Upvotes: 2

nishit dey
nishit dey

Reputation: 458

Can you use the below code

Msgbox Format("24/01/2017","MMM, DD, YYYY")

This will give you output as Jan, 24, 2017 . You can use other format which is easily available when you click on the Format cells. Thanks

Upvotes: 0

Related Questions