Yasser Moussa
Yasser Moussa

Reputation: 2329

current time with seconds

I have strange problem .. i have a function to display the time with seconds counter but i sent specific time (hours and minutes) as parameters when count of second reach 59 no minutes incremented and i am not sure if the same problem with hours when minutes will be 59 minute

function updateClock(current_hours,current_minutes) {
var currentTime = new Date ( );

var currentHours    = current_hours;
var currentMinutes  = current_minutes;
var currentSeconds  = currentTime.getSeconds ( );

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

// Choose either "AM" or "PM" as appropriate
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

// Convert the hours component to 12-hour format if needed
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

// Convert an hours component of "0" to "12"
currentHours = ( currentHours == 0 ) ? 12 : currentHours;

// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

// Big font time
$('.big_time').html("<span></span>" + currentTimeString);

// Time in red triangle in the track bar
$('.time').html(currentTimeString);
}

I call this function in another file inside document ready like that .. i sent here the data retrieved from an API (hours and minutes)

current_hours   = data.current_hours;
current_minutes = data.current_minutes;

setInterval(function(){updateClock(current_hours,current_minutes)},1000);

Upvotes: 1

Views: 378

Answers (2)

Roger
Roger

Reputation: 1693

Use this instead

function updateClock ( )
    {
    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
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

    // Choose either "AM" or "PM" as appropriate
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

    // Convert the hours component to 12-hour format if needed
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

    // Convert an hours component of "0" to "12"
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;

    // Compose the string for display
    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
    
    
    $("#clock").html(currentTimeString);
        
 }

$(document).ready(function()
{
   setInterval('updateClock()', 1000);
});

Got this code from here

Upvotes: 2

vyas
vyas

Reputation: 412

Please check the loop if it is going through 60 counts or ends at 59 itself for example for(i=1,i<=60:i++) it will go to 59 and ends,check for the start and end value.

Upvotes: 0

Related Questions