Reputation: 3377
I am creating a penny auction site and I have several auctions on the home page. Each auction gets its own timer based on a date from the mysql date format, which is in format 'Y-m-d H:i:s'.
The code works fine and the timers all start counting down perfectly on Firefox and Chrome, but in Safari the number shows NaN:NaN:NaN
Here's my function, its using the jquery countdown plugin by Keith Wood:
function updateTimers() {
$( ".il-timer" ).each(function( index ) {
var timer = $(this);
var end_date = $(timer).attr('data-end-date');
var auction_id = $(timer).attr('data-auction-id');
var end = new Date(Date.parse(end_date,"yyyy-mm-dd hh:mm:ss"));
$('#auction_listing_timer_'+auction_id).countdown({until: end, format: 'HMS', compact: true, description: ''});
//Do it every 10 seconds...
setTimeout(function(){
updateTimers();
}, 10000);
});
};
Upvotes: 2
Views: 7194
Reputation: 241525
A few things:
The Date.parse
function does not take a second parameter. The value "yyyy-mm-dd hh:mm:ss"
you're passing is being ignored.
Date parsing in JavaScript is implementation dependent. Firefox chooses not to parse values in that particular format.
new Date(Date.parse(...))
is redundant. You can just pass the string to the date constructor.
A quick fix would be to replace the dashes with slashes before parsing:
var end = new Date(end_date.replace(/-/g, '/'));
A better approach would be to use a library like moment.js, so you can control the format used for parsing:
var end = moment(end_date, 'YYYY-MM-DD HH:mm:ss').toDate();
Upvotes: 9