Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Location
    illinois
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript Format time

    I am new, please be patient.

    I am pulling in a start and end time from a database. I want to format the time so that if the start time and end time are both am, it will shorten the start time to only the hour (no minutes, no :, no AM/PM)

    For example:

    Before formatting

    Start time End time
    7:00 AM - 11:00 AM

    WHAT I WANT
    after formatting

    Start Time End Time
    7 - 11:00AM

    I have read through numerous forums, but none show how to change time from 7:00 AM to 7

    Any assistance would be greatly appreciated.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    It looks like your pulling the times from the DB as strings. All php string functions are here http://php.net/manual/en/ref.strings.php So it easier to do this in php then in javascript and why would you?

    PHP Code:
    <?php

    $start 
    '7:00 AM'// comes from database like this
    $end '11:00 AM'// ditto  Change this to PM and see what happens
    if(strpos($start'AM') && strpos($end'AM'))
        
    $start substr($start0strpos($start':'));
        echo 
    $start ' - ' $end;

    ?>
    Evolution - The non-random survival of random variants.

  • #3
    New to the CF scene
    Join Date
    Jul 2013
    Location
    illinois
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    in Javascript please

    The reporting software I am using only accepts Javascript. Do you know what the javascript code is that equates to your php example?

    I do not know php.

    The code below works if I was trying to pull the current time. But I don't want the current time, I want the time stored in the database.

    var d = new Date(); //current date and time
    d.getDate()
    d.getHours()

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    But show us how/where the reporting software dumps it into your HTML.

    Does it just dump it into, for example, a <span> or <div> or <td> in the format 7:00 AM - 11:00 AM? Is that what you are saying? And then you want to run an after-the-fact clean up? What about if it dumps in 10:32 AM - 1:15 PM??? How, if at all, do you want to modify that?
    Last edited by Old Pedant; 07-16-2013 at 11:15 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    devinjotyler, you said "I am pulling in a start and end time from a database." If you are using javascript to do this then OK, but if your using a server side languageto retrieve info then it's best to use that language.
    A straight translation into javascript:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body>
    <div id="open_time"></div>
    
    <script type="text/javascript">
    var start = '7:00 AM';
    var end = '11:00 AM';
    
    if((start.indexOf('AM') != -1) && (end.indexOf('AM') != -1))
    	document.getElementById('open_time').innerHTML = start.substring(0,start.indexOf(':'))+' - '+ end;
    else
    	document.getElementById('open_time').innerHTML = start+' - '+ end;
    </script>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Sunfighter: You are assuming that he gets the two times in separate fields.

    If they are in a single field, it might be easier to just use a regular expression.

    Or split the single field (if split weren't obsolete <grin/>).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Lol. Lets see what he says. He might reverse what he wants.
    Evolution - The non-random survival of random variants.

  • #8
    New to the CF scene
    Join Date
    Jul 2013
    Location
    illinois
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    She not he

    I attached a few snaphots. One is called startend and is a snapshot of three columns in the reporting software. Start time is one column. End time is second column. Calculated column is one I created that I need the help with.

    The second picture called calculated column shows you the exact interface I have to work with and where I can type the code.

    The reporting software is pulling the times from a unidata database that it is linked with. I don't know how or in what format, nor do i need to know. All I know is that I have a window where i can write javascript code to do what i want. It does not allow any othercode except javascript.

    The start time data in the database is a multivalued calculated column. Therefore, in the reporting software would I treat this as a string and use a .substring method?

    Also the reporting software only uses the main javascript code, it doesn't need all the header, title, etc that you would normally need for a webpage. for example the software recognizes this:


    //which does what the function i want, but only on the current time not a database stored time value

    var d = new Date();
    d.getHours()



    But does not recognize this formatting:

    <html>
    <head>
    <title>hello</title>
    </head>
    <body>
    var d = new Date();
    d.getHours()
    </body>
    </html>


    I probably just confused you.
    Attached Thumbnails Attached Thumbnails Javascript Format time-startend.jpg   Javascript Format time-calculatedcolumn.jpg  

  • #9
    New to the CF scene
    Join Date
    Jul 2013
    Location
    illinois
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    am pm clarification

    If the start time is 7:00am and end time falls in the pm say 1:00pm then nothing should be done.

    If the start and end are both pm, say 1:00pm - 3:00pm Then it needs to be shortened to show 1-3:00pm.

    Like wise if both times are am. 7:00am-8:00am should be 7-8:00am.

    Quote Originally Posted by Old Pedant View Post
    But show us how/where the reporting software dumps it into your HTML.

    Does it just dump it into, for example, a <span> or <div> or <td> in the format 7:00 AM - 11:00 AM? Is that what you are saying? And then you want to run an after-the-fact clean up? What about if it dumps in 10:32 AM - 1:15 PM??? How, if at all, do you want to modify that?

  • #10
    New to the CF scene
    Join Date
    Jul 2013
    Location
    illinois
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Solution

    var str=courseSections6_csmstarttime.toString() //database value to str
    str.substring(3);
    str.substring(0,2); //display only hours


    The above works. Now the only problem is it is pulling it back in military time instead of standard time.
    example
    1pm

    should be
    01

    instead shows up as
    13

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I think the problem with the kind of code you are using is that courseSections6_csmstarttime (for example) is an internal DATETIME format, *NOT* the string "7:00 AM" that you were referring to.

    But that's okay.

    I think we can do this.

    You didn't show the name of the endtime file, but I am assuming it is
    courseSections6_csmendtime

    If not, adjust my code to match the actual name.

    Code:
    function csmtm( hr )
    {
        var ampm = hr > 11 ? "pm" : "am"; // or upper case, as you prefer
        if ( hr > 12 ) hr -= 12;
        if ( hr == 0 ) hr = 12
        return hr + ":00" + ampm;
    }
    var csmsh = courseSections6_csmstarttime.getHours();
    var csmeh = courseSections6_csmendtime.getHours();
    if ( ( csmsh >= 12 && csmeh >= 12 ) || ( csmsh < 12 && csmeh < 12 ) )
    {
        if ( csmsh > 12 ) csmsh -= 12;
        if ( csmsh == 0 ) csmsh = 12;
        str = csmsh + "-" & csmtm(csmeh);
    } else {
        str = csmtm(csmsh) + "-" + csmtm(csmeh);
    }
    This assumes that this system you are using expects to find the formatted value in a string variable named str as demoed by what you showed.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    New to the CF scene
    Join Date
    Jul 2013
    Location
    illinois
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I am going to play around with what you sent, because it does what i want if the times are am/pm, but does not do what i want if the times are am/am or pm/pm. I have included a snapshot (titled csmtm) of the results of your code. The column titled Start Time (display hours only) show the output of your code

    I have also attached a screenshot (titled tohours) of results from my code, which does convert the time to only hours.The column titled Start Time (display hours only) show the output of my code

    somewhere in the middle is the answer. I will try some more today and let you know if i figure it out. Appreciate the help and ideas you have given me.

    -jo
    Attached Thumbnails Attached Thumbnails Javascript Format time-csmtm.jpg   Javascript Format time-tohours.jpg  

  • #13
    New to the CF scene
    Join Date
    Jul 2013
    Location
    illinois
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    my code

    var str=courseSections6_csmstarttime.toString()

    str.substring(3);
    str.substring(0,2);

    var hours = str.substring(0,2)

    // only -12 from hours if greater than 12
    hours = (hours> 12)? hours - 12: hours;

    // if 00 then it is midnight
    hours = (hours == '00')? 12 : hours;

    This is my code which only converts from military time to standard time and strips the start time to display hours only.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Must be a minor bug in my code. The zeroes that show there indicate to me that probably my concatenation is off. Let me look some more.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Oh, yes. Just a typo!

    On this line:
    Code:
        str = csmsh + "-" & csmtm(csmeh);
    the & should be + instead.

    Code:
        str = csmsh + "-" + csmtm(csmeh);
    Then it seems to work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •