Military Time Problem

S

Scott

If I have a datetime sql field with afternoon datetime values like below in
DATETIME VALUE, how can I display the time part in ASP as 1:00 PM, instead
of 13:00 PM as seen in CURRENT RESULTS below?

Currently, I'm getting my CURRENT RESULTS by using the HOUR() function. Is
there a way to display just the time part in "non-Military" time format?


DATETIME VALUE:
2006-01-24 13:30:00.000

CURRENT RESULTS::
13:30 PM

DESIRED RESULTS:
1:30 PM
 
D

dNagel

I've worked up an example that procduced the desired result .. it is
_slightly tested_ ...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<pre>
You are going to experience problems with this method unless you format your
Date from SQL in an acceptable format ( as presented below ) before it hits
your JS code.

01-24-2006 13:30:00
</pre>

<script>
// this is where you format your date with server side code
// I'm using your hard coded value for now

var someDate= new Date('01-24-2006 13:30:00');

document.write ( '<pre>' + someDate.toString() + '<br />')

with (someDate)
{
document.write ( getHours() % 12 + ':' + getMinutes() +
( parseInt(getHours()) > 12 ? ' PM' : ' AM') + '</pre>')
}

</script>
</body>
</html>

hth,

D.
 
D

dNagel

If you do this a lot you might want to consider adding a new prototype to
the Date object...

<script>

Date.prototype.humanTime = function () {
var sDate = new String();
sDate = ( this.getHours() % 12 + ':' + this.getMinutes() + ( parseInt(this.getHours()) > 12 ? ' PM' : ' AM') )
return (sDate.toString())
}
var someDate= new Date('01-24-2006 13:30:00');
document.write ( '<pre>' + someDate.toString() + '<br />')
document.write ( someDate.humanTime() + '<br />')

var sNow = new Date()
document.write ( 'The time is now : ' + sNow.humanTime() )

document.write ( '</pre>' )

</script>

hth,

D.
 
D

dNagel

ok, too much beer tonight...

I blew it on 12am , and 12pm...

replace the prototype with this ...


Date.prototype.humanTime = function () {
var sDate = new String(), sHour = new String(), sAMPM = new String();
sHour = this.getHours() % 12 ;
sAMPM = parseInt(this.getHours()) > 11 ? ' PM' : ' AM';
if (sHour == 0) sHour = 12;
sDate = sHour + ':' + this.getMinutes() + sAMPM;
return (sDate.toString());
}

D.
 
S

Scott

thanks guys.

dNagel said:
ok, too much beer tonight...

I blew it on 12am , and 12pm...

replace the prototype with this ...


Date.prototype.humanTime = function () {
var sDate = new String(), sHour = new String(), sAMPM = new String();
sHour = this.getHours() % 12 ;
sAMPM = parseInt(this.getHours()) > 11 ? ' PM' : ' AM';
if (sHour == 0) sHour = 12;
sDate = sHour + ':' + this.getMinutes() + sAMPM;
return (sDate.toString());
}

D.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top