Results 1 to 5 of 5

Thread: [RESOLVED] converting custom date/time value

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    4

    Resolved [RESOLVED] converting custom date/time value

    I'd appreciate some help on this, as I'm stuck.

    I've have an excel workbook that uses macros to pull data from a company database. Almost all records have a date/time value associated with it.

    Most date/time values use the format dd-mmm-yy hh:mm:ss (10-Dec-05 08:50:25)

    My problem is that some tables are storing records in a similar format, but the values come out as 10-Dec-05 08:50:25.000
    I'm trying to find records by more/less than queries, but it won't work with those zero values on the end.

    How do I get rid of those three zeros on the end so that my macro recognises it as a date/time value?

    Cheers,
    Carl

  2. #2
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: converting custom date/time value

    Through VBA, use the following code.
    VB Code:
    1. Range("A1").NumberFormat = "dd-mmm-yy hh:mm:ss"
    This will set the number format of A1 cell to "dd-mmm-yy hh:mm:ss" format.

    If you do not want to use VBA then,
    Select the cells. Go to Format->Cells. Select the Number tab then Select custom and then enter type as "dd-mmm-yy hh:mm:ss".

    Or, you can do one more thing. If you pull the data from database using vb/vba code, then you can use the format function to change the format of the date and time to your format. Let's say,
    VB Code:
    1. strDateTime="10-Dec-05 08:50:25.000"
    2. Msgbox Format(strDateTime,"dd-mmm-yy hh:mm:ss")
    Last edited by cssriraman; Dec 10th, 2005 at 02:40 AM.
    CS

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    4

    Re: converting custom date/time value

    Thanks but i'd already tried that and got Type Mismatch.

    Here's what I'm doing

    VB Code:
    1. Set rstTemp = qdfTemp.OpenRecordset()
    2. strDateTime = rstTemp.Fields("Time & Date")
    3. MsgBox (strDateTime)   'gives me 10-Dec-05 08:50:25.000
    4. Msgbox Format(strDateTime,"dd-mmm-yy hh:mm:ss")   'gives me Type Mismatch

    I think I have to strip off the trailing zeros before trying to format as a Date & Time. I haven't been able to do that.

    Cheers,
    Carl

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: converting custom date/time value

    This should help:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4. Dim x As Integer
    5. Dim strdatetime As String
    6. strdatetime = "10-Dec-05 08:50:25.000"
    7. MsgBox (strdatetime)   'gives me 10-Dec-05 08:50:25.000
    8. x = InStrRev(strdatetime, ".") - 1
    9. If x > 0 Then
    10.  strdatetime = Left(strdatetime, x)
    11. End If
    12. MsgBox Format(strdatetime, "dd-mmm-yy hh:mm:ss")
    13. End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    4

    Thumbs up Re: converting custom date/time value

    Thanks very much. That was just was I needed.

    Cheers,
    Carl

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