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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Google Calendar Help Time Formatting

    I don't want to have the time displayed as 24 hours and I know that the "getHours" makes it do that. What do I need to edit in Google's coding to change that? This is the portion of the code that displays the event.

    Code:
      /* loop through each event in the feed */
      var len = entries.length;
      for (var i = 0; i < len; i++) {
        var entry = entries[i];
        var title = entry.getTitle().getText();
        var startDateTime = null;
        var startJSDate = null;
        var times = entry.getTimes();
        if (times.length > 0) {
          startDateTime = times[0].getStartTime();
          startJSDate = startDateTime.getDate();
        }
        var entryLinkHref = null;
        if (entry.getHtmlLink() != null) {
          entryLinkHref = entry.getHtmlLink().getHref();
        }
        var dateString = (startJSDate.getMonth() + 1) + "/" + startJSDate.getDate();
        if (!startDateTime.isDateOnly()) {
          dateString += " " + startJSDate.getHours() + ":" + 
              padNumber(startJSDate.getMinutes());
        }
        var li = document.createElement('li');
    Last edited by mattellerin; 08-16-2012 at 10:16 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    Generalized function:
    Code:
    function ampmTime( datetime )
    {
        var hr = datetime.getHours();
        var ampm = hr >= 12 ? " pm" : " am";
        if ( hr > 12 ) hr -= 12;
        if ( hr == 0 ) hr = 12;
        var mn = dateTime.getMinutes();
    
        return hr + ":" + ( mn < 10 ? "0" : "" ) + mn + ampm;
    }
    So replace
    Code:
    if (!startDateTime.isDateOnly()) {
        dateString += " " + startJSDate.getHours() + ":" + padNumber(startJSDate.getMinutes());
    }
    with
    Code:
    if (!startDateTime.isDateOnly()) {
        dateString += " " + ampmTime( startJSDate );
    }
    Try it.
    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.

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Now my events are not listed at all. Did I input your suggestion incorrectly? Thank you for the help.

    Code:
      /* loop through each event in the feed */
    function ampmTime( datetime )
    {
        var hr = datetime.getHours();
        var ampm = hr >= 12 ? " pm" : " am";
        if ( hr > 12 ) hr -= 12;
        if ( hr == 0 ) hr = 12;
        var mn = dateTime.getMinutes();
    
        return hr + ":" + ( mn < 10 ? "0" : "" ) + mn + ampm;
    }
       
      var len = entries.length;
      for (var i = 0; i < len; i++) {
        var entry = entries[i];
        var title = entry.getTitle().getText();
        var startDateTime = null;
        var startJSDate = null;
        var times = entry.getTimes();
        if (times.length > 0) {
          startDateTime = times[0].getStartTime();
          startJSDate = startDateTime.getDate();
        }
        var entryLinkHref = null;
        if (entry.getHtmlLink() != null) {
          entryLinkHref = entry.getHtmlLink().getHref();
        }
        var dateString = (startJSDate.getMonth() + 1) + "/" + startJSDate.getDate();
        if (!startDateTime.isDateOnly()) {
        dateString += " " + ampmTime( startJSDate );
    }    
         var li = document.createElement('li');

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,333 Times in 4,299 Posts
    SORRY! A typo on my part, but you could have easily seen it if you had looked at your error console.

    Code:
        var mn = dateTime.getMinutes();
    Upper case "T" instead of lower case "t". Should of course be:
    Code:
        var mn = datetime.getMinutes();
    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.

  • Users who have thanked Old Pedant for this post:

    mattellerin (08-17-2012)

  • #5
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you, I really appreciate you taking the time to help me out.


  •  

    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
    •