HELP ASAP !! How do you convert DD/MM/YYYY to MM/DD/YYYY in VB6

I am taking dates input as DD/MM/YYYY and converting them internally.  At first I thought the format function would work.

I used the format("15/01/2005","MM/DD/YYYY") and that works, converting it to "01/15/2005"

Then I tried another date...  format("01/05/2005","MM/DD/YYYY")  which should have converted to 05/01/2005 but the output was "01/05/2005"

Is there a way to make it work for all dates and not just those with a day greater than 12?

I am giving 500 points for the answer to this.
SethCrews_1Asked:
Who is Participating?
 
ShauliConnect With a Mentor Commented:
You need to use DateSerial for that:

Private Sub Command1_Click()
MsgBox ConvertDate("01/05/2005", "MM/DD/YYYY")
End Sub

Public Function ConvertDate(ByVal fnDate As Date, fnFormat As String)
Dim dSplit() As String, tmpDate As Date
dSplit = Split(fnDate, "/", -1)
If UBound(dSplit) > 0 Then
    tmpDate = DateSerial(dSplit(2), dSplit(1), dSplit(0))
End If
ConvertDate = Format(tmpDate, fnFormat)
End Function

S
0
 
Robberbaron (robr)Commented:
Converting dates is a real problem as the Code needs to know if the input is in what format.
Shaulis code should work but the function should read

Public Function ConvertDate(ByVal fnDate As String, fnFormat As String) as String
  'note assumes input as dd/mm/yyyy
  'perhaps some checking to use format is correct.
  if instr("/-.",mid(fndate,3,1)) then
      'looks ok and we now have separator used
      sepchar=mid(fndate,3,1))
    else
       'dodgy format  ' return BAD
  end if
...
  dSpilt = split(fndate,sepchar,-1)
....


       
because you dont want VB to autoconvert input as VB uses the short date & regional setting s to determine what to do with 01/02  ie is it 01/Feb in Australia or 02/Jan in the US ?

Provided you clearly note to the user the format required ie dd/mm   including /  not - or .  code should work well.
perhaps function should be called ConvertDate_ddmmyyyy(....)  so you can use a different format if desired. ie a user setting.

As an aside, i generally use dd/mmm/yyyy for all my output at least, so there is absolutely no confusion.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.