www.webdeveloper.com
Results 1 to 2 of 2

Thread: Problem displaying date and time correctly..

  1. #1
    Join Date
    Nov 2004
    Posts
    121

    Problem displaying date and time correctly..

    I have a problem when I want to display a date with hours, minutes, seconds.

    This is the way it looks now.
    2005-7-16 12:14:6

    but it should be 2005-07-16 12:14:06 instead.

    How can I get the code to add a zero every time any of the numbers (month, date, hour, minute, second) is smaller than 10?

    I use this code to display everything when the date is retrieved from my db.

    Code:
    <%=Year(rs("reg_date"))%>-<%=Month(rs("reg_date"))%>-<%=Day(rs("reg_date"))%>&nbsp;&nbsp;<%=hour(rs("reg_date"))%>:<%=minute(rs("reg_date"))%>:<%=second(rs("reg_date"))%>

  2. #2
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    This is a common problem associated with dates. I usually write a small function to prefix a zero to the date/time part if necessary.
    Code:
    Public Function PrefixZero(p_strDatePart)
        If Not IsNumeric(p_strDatePart) Then PrefixZero = p_strDatePart: Exit Function
        
        If CInt(p_strDatePart) < 10 Then
            PrefixZero = "0" & p_strDatePart
        Else
            PrefixZero = p_strDatePart
        End If
    End Function
    You can pass each date/time part to that function.
    Code:
    Dim strHour
    strHour = PrefixZero(Hour(rs("reg_date")))
    Regards.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles