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

Posted on 2005-05-02
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

    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)
    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now