我有一个代码将数据放入数据表中并进行时间计算:
$.each(myObj[1], function(key,value) {
var date_start = new Date(value.start_timestamp);
var date_end = new Date(value.end_timestamp);
var milisec = Math.abs(date_end - date_start);
var seconds = milisec / 1000;
var hours = parseFloat( seconds / 3600 );
totalhours += hours;
var isdaily = '';
if(value.is_daily == 0)
{
isdaily = 'Added'
}
else
{
isdaily = 'Daily'
}
t.row.add( [
value.id,
value.task_description,
value.start_timestamp,
value.end_timestamp,
hours.toFixed(2)+" hr(s)",
value.name,
value.assign,
value.created_at,
isdaily,
value.status
] ).draw();
});
var final_compute = Math.ceil(totalhours.toFixed(2));
$('#totalhours').text(final_compute);
alert("Tasks from the last 12 hours generated.");
我的变量milisec, seconds, hours
有NaN
值,会发生什么。结果是date_start
和date_end
导致无效日期造成的。但是在Chrome上运行正常。我的时间戳格式类似于2016-05-04 23:00:00