Convert string to date

  • Thread starter Thread starter Guest
  • Start date Start date
ItsADate = CDate(Left("05262007",2) & "/" & Mid("05262007",3,2) & "/" &
Right("05262007",4))
 
Actually, it's even easier than that:

ItsADate = CDate(Format("05262007", "##/##/####"))

Of course, this method (and yours) will only work when the user's Short Date
format is mm/dd/yyyy (or something like yyyy-mm-dd or dd mmm yyyy). In other
words, it won't work if the user's Short Date format is dd/mm/yyyy, since
CDate is one of the few built-in Date functions that actually respects the
user's settings.

To be absolutely certain, use

ItsADate = DateSerial(Right("05262007",4), Left("05262007",2),
Mid("05262007",3,2))
 
Douglas J. Steele said:
Actually, it's even easier than that:

ItsADate = CDate(Format("05262007", "##/##/####"))

Of course, this method (and yours) will only work when the user's Short
Date format is mm/dd/yyyy (or something like yyyy-mm-dd or dd mmm yyyy).
In other words, it won't work if the user's Short Date format is
dd/mm/yyyy, since CDate is one of the few built-in Date functions that
actually respects the user's settings.

To be absolutely certain, use

ItsADate = DateSerial(Right("05262007",4), Left("05262007",2),
Mid("05262007",3,2))

Good approaches, Doug. Thanks.
 
Back
Top