Military Time Problem

Discussion in 'ASP General' started by Scott, Jan 25, 2006.

  1. Scott

    Scott Guest

    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
     
    Scott, Jan 25, 2006
    #1
    1. Advertising

  2. Scott

    dNagel Guest

    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, Jan 25, 2006
    #2
    1. Advertising

  3. Scott

    dNagel Guest

    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, Jan 25, 2006
    #3
  4. Scott

    dNagel Guest

    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.
     
    dNagel, Jan 25, 2006
    #4
  5. Scott

    Scott Guest

    thanks guys.

    "dNagel" <> wrote in message
    news:...
    > 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.
     
    Scott, Jan 27, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. P. Flavin
    Replies:
    1
    Views:
    6,019
    P. Flavin
    Sep 12, 2003
  2. Phillip Vong

    DataFormatString Military Time

    Phillip Vong, Dec 19, 2006, in forum: ASP .Net
    Replies:
    8
    Views:
    8,908
    Phillip Vong
    Dec 21, 2006
  3. flamesrock
    Replies:
    8
    Views:
    502
    Hendrik van Rooyen
    Nov 24, 2006
  4. Replies:
    1
    Views:
    1,430
    Roedy Green
    Sep 22, 2007
  5. David Hearn
    Replies:
    1
    Views:
    424
    David Hearn
    Sep 10, 2003
Loading...

Share This Page