Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2005-05-02
Medium Priority
Last Modified: 2008-03-04
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.
Question by:SethCrews_1
LVL 19

Accepted Solution

Shauli earned 1500 total points
ID: 13908398
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

LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 13908516
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
       '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.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question