Results 1 to 2 of 2

Thread: Problem in Date format

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Problem in Date format

    Hi,

    I got a problem with the date format for that I need work around.

    I have a date in dd-mm-yyyy hh:mm:ss format. I simply want to convert into mm/dd/yyyy hh:mm format.

    I used format function. but it is not coverting the date properly. For example,
    VB Code:
    1. Debug.print Format("02-05-2006 20:53:56", "mm/dd/yyyy HH:mm")
    2.  
    3. 02/05/2006 20:53 'This should be 05/02/2006 20:53
    4.  
    5. Debug.print Format("18-05-2006 20:53:56", "mm/dd/yyyy HH:mm")
    6.  
    7. 05/18/2006 20:53 'This is correct. no issues.

    Somebody help me on this please.

    Thanks,

    CS.
    CS

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Problem in Date format

    You'll have to manually convert them. When you try to cast a string to a date, it will use your regional settings to determine the date and month. The reason your second example works is because VB is smart enough to figure out that there is no month "18", and assumes it's the day. This should do the trick:
    VB Code:
    1. Private Sub TryIt()
    2.  
    3.     Dim sTest As String
    4.  
    5.     sTest = DateConvert("02-05-2006 20:53:56")
    6.     Debug.Print sTest
    7.     Debug.Print Format$(sTest, "General Date")
    8.     Debug.Print Format$(sTest, "mm/dd/yyyy HH:mm")
    9.  
    10. End Sub
    11.  
    12. Private Function DateConvert(sInput As String) As Date
    13.  
    14.     Dim sDateTime() As String, sDate() As String
    15.  
    16.     sDateTime = Split(sInput, " ")
    17.     sDate = Split(sDateTime(0), "-")
    18.     DateConvert = DateValue(sDate(1) & "/" & sDate(0) & "/" & sDate(2))
    19.     DateConvert = DateConvert + CDate(sDateTime(1))
    20.  
    21. End Function
    Last edited by Comintern; May 18th, 2006 at 01:51 PM. Reason: typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width