Link to home
Start Free TrialLog in
Avatar of AlexF777
AlexF777

asked on

What would be the quickest way to Format "Mar 24 2005" => "03/24/2005"

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudheeshthegreat
sudheeshthegreat

to be quicker ;-)

Format("Mar 24 2005", "mm/dd/yyyy")
MsgBox CDate("Mar 24 2005")

S
define quick:

time it takes to type
time it takes the CPU to do

?
Option Explicit

Private Sub Form_Load()
  Dim start As Double
  Dim iter As Long
  Dim s As String
 
  start = DateTime.Timer
  For iter = 1 To 100000
    s = CDate("24 Mar 2004")
  Next iter
  Debug.Print "CDATE method took " & DateTime.Timer - start & " sec"
 
  start = DateTime.Timer
  For iter = 1 To 100000
    s = Format("24 Mar 2004", "mm/dd/yyyy")
  Next iter
  Debug.Print "FORMAT method took " & DateTime.Timer - start & " sec"
 
  start = DateTime.Timer
  For iter = 1 To 100000
    s = Format(CDate("Mar 24 2005"), "mm/dd/yyyy")
  Next iter
  Debug.Print "FORMATCDATE method took " & DateTime.Timer - start & " sec"
 
  start = DateTime.Timer
  For iter = 1 To 100000
    s = Format("24 Mar 2004", "mm/dd/yyyy")
  Next iter
  Debug.Print "FORMAT$ method took " & DateTime.Timer - start & " sec"
 
End Sub



my laptop's results are:
CDATE method took 0.657281250001688 sec
FORMAT method took 0.797156250002445 sec
FORMATCDATE method took 0.703281249996508 sec
FORMAT$ method took 0.797156250002445 sec


about 3 seconds to convert 400,000 dates... i think youll have other issues in your data prep than this
ps, i made an error, not putting Format$ on the last bit of code, only the string printout.. but it makes no difference, infact my Format$ took longer
Agreed that any of the above works well enough with the exception of:
s = CDate("24 Mar 2004")

Without using the Format function, the returned format will be the system default short date format.  This may not be in the form "mm/dd/yyyy"
<<< This may not be in the form "mm/dd/yyyy">>>
That is true of course, if the locale is not in the format of "mm/dd/yyyy". However, if the locale IS in that format, then this would be the fastest way.

S
assuming the locale having a format of mm/dd/yyyy would, i believe, be a big assumption. why would you want to convert dates from one format to another then?