PDA

View Full Version : Removing seconds from SQL Server time


bspahr
01-31-2008, 06:12 PM
I am displaying a time on a asp page from SQL server.

The code is like this:
<%= formatdatetime(rsDel("DelTime"),3)%>


The time displays as: 5:00:00 AM
I want it to display as: 5:00 AM

Is there a way to remove the seconds?

glenngv
01-31-2008, 07:04 PM
You can extract the short date and short time by passing 2 and 4 respectively as 2nd parameter to FormatDateTime function. But the short time is in 24-hr format, convert it if you want the time to have 12-hr format.
<%
dim delTime
delTime = rsDel("DelTime")
delTime = FormatDateTime(delTime, 2) & " " & FormatDateTime(delTime, 4)
response.write delTime
%>
Check this out for more info of the FormatDateTime function.

http://devguru.com/technologies/vbscript/13925.asp

bspahr
01-31-2008, 07:22 PM
That code displayed this:
12/30/1899 05:00

glenngv
01-31-2008, 07:58 PM
I told you the time extracted is displayed in military format (24-hr). You need to convert it to 12-hr if you want.

glenngv
01-31-2008, 08:04 PM
Or you can simply use this if all the times in the database have 00 seconds.

<%= Replace(FormatDateTime(rsDel("DelTime"),3), ":00 ", "")%>

bspahr
01-31-2008, 09:07 PM
Yes, all times currently have 00 seconds. This displays them correctly. Thanks.

What if all the seconds were differrent though?
Would it still be possible to replace them?
Was just curious. Thanks again for your help.

glenngv
01-31-2008, 11:01 PM
Then use my original solution.

bspahr
02-01-2008, 05:59 PM
When I paste your original code into my page it displays the date and time like this:
12/30/1899 05:00

instead of just the time like this: 05:00 AM

Am I missing something?

Spudhead
02-01-2008, 06:30 PM
Did you visit the FormatDateTime() reference page that glenngv put in his original post?

http://devguru.com/technologies/vbscript/13925.asp

glenngv
02-01-2008, 07:08 PM
Try this:
Function GetTime(dateTime)
dim hr, min, ampm

ampm = " PM"

hr = Hour(dateTime)
if hr>12 then
hr = hr-12
elseif hr<12 then
ampm=" AM"
if hr=0 then
hr=12
elseif hr<10 then
hr="0"&hr
end if
end if

min = Minute(dateTime)
if min<10 then min="0"&m

GetTime = hr & ":" & min & ampm
End Function

Response.Write GetTime(rsDel("DelTime"))