Skip to main content
Inspiring
August 17, 2006
Question

Time Format in ASP/Javascript

  • August 17, 2006
  • 3 replies
  • 331 views
I'm trying to format time in an ASP/javascript page in Dreamweaver that is being pulled from an Access 2003 database. The default format has seconds displayed which I do not want. In Dreamweaver you can choose formatting, however, none of the choices are what I need. What I want is non-military time, no seconds displayed and I do want AM/PM. So, this is what I would like to see:

4:45 PM

The Dreamweaver formatting gives me the following:

04:45:00 PM

04:45:00

16:45

None of these are useful to me. Does anyone know how I can get my time to display correctly?

thanks,
Winona
This topic has been closed for replies.

3 replies

Inspiring
August 22, 2006
You should be able to drop this code onto the page where you want the time
to appear.
This is just a quick and dirty chunk of code that I put together and have
not tested.
Please bear in mind that I do not code in ServerSide JavaScript (I use
VBScript) so the code might not be accurate...

<%
var now = (appointment.Fields.Item("AP_AppointTime").Value)
var hour = now.getHours();
var minute = now.getMinutes();
var ampm = "AM";
if (hour>11){
ampm = "PM";
}
response.write(hour + ":" + minute + " " + ampm);
%>

I hope that helps.
Cheers,
Rob
http://robgt.com/ [Tutorials and Extensions]
Firebox stuff: http://robgt.com/firebox
Skype stuff: http://robgt.com/skype
SatNav stuff: http://robgt.com/satnav



WinonaAuthor
Inspiring
August 17, 2006
Unfortunately I'm a novice and I don't know enough about javascript to pull it together. I'm using Dreamweaver 8, ASP/javascript and an Access 2003 database. This is the field being pulled in using ASP:

<%=(appointment.Fields.Item("AP_AppointTime").Value)%>

My question is how would I write the javascript to use that field as a variable then how would I call in the javascript?
Inspiring
August 17, 2006
You should be able to use some code from this page and turn it into what you
need.

http://www.web-source.net/web_development/javascript_date.htm

It's not all applicable as you do not want to create the date object, as
that code does, but the principles you need to employ are the same for
getting the hour and minute portions out of your stored date and also
working out the AM PM part and displaying that too.
I hope that helps.
Cheers,
Rob
http://robgt.com/ [Tutorials and Extensions]
Firebox stuff: http://robgt.com/firebox
Skype stuff: http://robgt.com/skype
SatNav stuff: http://robgt.com/satnav