Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format

Status
Not open for further replies.

LCD

MIS
Feb 19, 2001
72
US
I'm trying to format the Date/Time so it shows mmddhhmmss with each taking up to 2 chars. (so it'd look like 0708065822 instead of 7865822)

strDOB = month(date()) & day(date()) & hour(now()) & minute(now()) & second(now())

is what I'm currently using. Any suggestions? All I can think of is a bunch of IF ELSE Statements or a Function. Is there a anything like Format(Month(Date()), ##)?

Thanks,
Andrew
 
Unfortunately there is no format function like that. FormatDate() will format it into specific formats but none of them are zero-padded like that. A few If-Thens will take care of it, though (placed in a function if you need to do it more than once).
 
Code:
Function PadDate(DateIn)
    Dim mm, dd, hh, nn, ss
    mm = Month(DateIn)
    If mm < 10 Then mm = &quot;0&quot; & mm
    dd = Day(DateIn)
    If dd < 10 Then dd = &quot;0&quot; & dd
    hh = Hour(DateIn)
    If hh < 10 Then hh = &quot;0&quot; & hh
    nn = Minute(DateIn)
    If nn < 10 Then nn = &quot;0&quot; & nn
    ss = Second(DateIn)
    If nn < 10 Then ss = &quot;0&quot; & ss
    PadDate = mm & dd & hh & nn & ss
End Function
It's better than the If-Then-Else method because it requires less string concatenation which should save processing time over the five &quot;PaddedDate = PaddedDate & x&quot; type of things.
 
This is about as short as I could make it
<%
Function leadDigit(val)
If Len(val) = 1 Then
val = &quot;0&quot; & val
End If
leadDigit = val
End Function

Dim strDOB
'format to mmddhhmmss
strDOB = leadDigit(Month(now)) & leadDigit(Day(now)) & leadDigit(Hour(now))
strDOB = strDOB & leadDigit(Minute(now)) & leadDigit(Second(now))

Response.Write strDOB
%>

remember this is not a valid date time format for as far as I know anything

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
onpnt, your version would run faster if you replaced the Len(val) with val < 10 -- numeric comparison will be faster than virtually anything with strings, including Len() (even though it's effectively a pointer operation). Or so I've been told.
 
'I ended up using

Function leadDigit(val)
If val < 10 Then
val = &quot;0&quot; & val
End If
leadDigit = val
End Function

strDOB = Year(now) & leadDigit(month(now)) & leadDigit(day(now)) & _
leadDigit(hour(now)) & leadDigit(minute(now)) & leadDigit(second(now))

'Genimuse and onpnt, Thank you both for the help.
 
[smile]

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top