function show_elapsed_time(from)
{
var time_elapsed = 开发者_如何转开发new Date().getTime()-from;
var date = new Date(time_elapsed);
var date_elements = (pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds())).split('');
var date_string = '';
for(i = 0; i < date_elements.length; i++)
{
date_string += '<div class="frame">' + date_elements[i] + '</div>';
}
$('#digital_clock').html(date_string);
}
show_elapsed_time(1308446047*1000)
The expected result (at the time of this post) is 0 hours, 2 minutes, .. seconds. This is as well the result given by Opera, Chrome and IE. However, Firefox returns 1 hour, 2 minutes, etc. How to fix it?
Basically:
var date = new Date(1453288); console.log(date.getHours()); // FF: 1, IE: 0, Opera 0
What I am doing is taking: new Date().getTime() - [some timestamp] == time passed
So I need to know time passed from [some timestamp]
in hours, minutes and seconds.
Well, the problem is with different locale browser settings. One way to solve the problem is to use UTC
specific methods, e.g. getUTCTime()
. Instead, I've written this small script to do the math:
var time_elapsed = new Date().getTime()-from;
var hours = Math.floor(time_elapsed/(3600*1000));
time_elapsed -= hours*(3600*1000);
var minutes = Math.floor(time_elapsed/(60*1000));
time_elapsed -= minutes*(60*1000);
var seconds = Math.floor(time_elapsed/1000);
精彩评论