+ Reply
Results 1 to 1 of 1

Your favorite Apple, iPhone, iPad, iOS, Jailbreak, and Cydia site.


Thread: Need a second pair of eyes on some Javascript

is a discussion within the

Skinning / Themes Discussion

forums, a part of the

Design and Media For the iPhone / iPod Touch

section;
This is the code for the clock on my current lockscreens. Everything works perfectly, but for one of my themes that I created I need the 0(zero) to be in
...
  1. #1
    iPhone? More like MyPhone wardever's Avatar
    Join Date
    Feb 2013
    Location
    Chicago
    Posts
    154
    Thanks
    185
    Thanked 375 Times in 112 Posts

    Question Need a second pair of eyes on some Javascript
    This is the code for the clock on my current lockscreens. Everything works perfectly, but for one of my themes that I created I need the 0(zero) to be in front of the single digit hour times (ie. 01:00 AM). My code below works for the single digit hour numbers for AM, but when it switches to PM it goes back to single digit with no zero in front of it. I can not figure out how to get the zero to appear when it has switched to PM. Any Javascript geniuses out there? Thank you in advance.

    <script type="text/javascript">

    function updateClock ( )
    {
    var currentHours_name_array = new Array ("12", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")
    var currentMinutes_name_array = new Array ("00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28","29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "00")
    var currentTime = new Date ( );
    var currentHours = currentTime.getHours ( );
    var currentMinutes = currentTime.getMinutes ( );
    var currentSeconds = currentTime.getSeconds ( );

    <!-- Pad the minutes and seconds with leading zeros, if required -->
    currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

    <!-- Defines either "AM" or "PM" as appropriate -->
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

    <!-- Convert hours component of "12" to "24" -->
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

    <!-- Convert hours component from "0" to "12 at Midnight-->
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;

    <!-- Compose the string for display-->
    var currentTimeString = currentHours + ":" + currentMinutes;

    <!-- Update the time display-->
    document.getElementById("clock").firstChild.nodeVa lue = currentTimeString;
    }

    function amPm ( )
    {
    var currentTime = new Date ( );
    var currentHours = currentTime.getHours ( );

    <!-- Defines either "AM" or "PM" as appropriate -->
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

    <!-- Convert hours component of "12" to "24" -->
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

    <!-- Convert hours component from "0" to "12 at Midnight-->
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;

    <!-- Compose the string for display-->
    var currentTimeString = timeOfDay;

    <!-- Update the string for display of AM/PM-->
    document.getElementById("ampm").firstChild.nodeVal ue = currentTimeString;
    }
    Last edited by wardever; 02-25-2013 at 04:58 PM.

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