For Programmers: Free Programming Magazines  


Home > Archive > ASP > January 2006 > Military Time Problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Military Time Problem
Scott

2006-01-24, 9:55 pm

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


dNagel

2006-01-25, 3:56 am

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.

Scott wrote:
> 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
>
>

dNagel

2006-01-25, 3:56 am

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.
dNagel

2006-01-25, 3:56 am

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.


dNagel wrote:
> 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.

Dave Anderson

2006-01-25, 7:55 am

"Scott" wrote:
> 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?


You don't mention the SQL flavor, but I would think you could obtain the
desired format in SQL before trying to deal with it:

http://msdn.microsoft.com/library/e..._ca-co_2f3o.asp





--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.


Scott

2006-01-27, 6:55 pm

thanks guys.

"dNagel" <NOTGrandNagel@NotMail.com> wrote in message
news:uF34rdYIGHA.2628@TK2MSFTNGP15.phx.gbl...[color=darkred]
> 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.
>
>
> dNagel wrote:


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com