Time Formatting

1. Member
Join Date
Dec 1969
Posts
68

## Time Formatting

When I am using FormatDateTime, the time constant 3 gives me seconds and 4 gives me military time. Is there a way that I can get hh:mm am/pm?<BR><BR>Easy one.

2. Senior Member
Join Date
Dec 1969
Posts
5,955

## What does the documentation say? ...

...<BR><BR>http://msdn.microsoft.com/library/en-us/script56/html/vsfctFormatDateTime.asp<BR><BR>Oliver.

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## The documentation says no...

...but it&#039;s not too hard to do it.<BR><BR>Two ways:<BR><BR>&#060;%<BR>Function FormatTime( tm )<BR> tm = CDate( tm ) &#039; just in case<BR> Dim hr, ampm<BR> hr = Hour(tm)<BR> If hr &#062; 11 Then ampm = " PM" Else ampm = " AM"<BR> If hr &#062; 12 Then hr = hr - 12<BR> If hr = 0 Then hr = 12<BR> FormatTime = hr & ":" & Right("0" & Minute(tm), 2) & ampm<BR>End Function<BR>%&#062;<BR><BR>If you always want two digit hours, then change the penultimate line:<BR><BR> FormatTime = Right("0" & hr, 2) & ":" & Right("0" & Minute(tm), 2) & ampm<BR>End Function<BR><BR>The other slightly sneakier way:<BR><BR>&#060;%<BR>Function FormatTime( tm )<BR> tm = CDate( tm ) &#039; just in case<BR> Dim txt, re<BR> txt = FormatDateTime( tm, 3 )<BR> re = New RegExp<BR> re.Pattern = ":dd "<BR> FormatTime = re.Replace( txt, " " )<BR>End Function<BR>%&#062;<BR><BR>

4. Member
Join Date
Dec 1969
Posts
68

## I checked the documentation already...

That&#039;s why I&#039;m asking everyone else.

5. Senior Member
Join Date
Dec 1969
Posts
96,118