两个日期时间跨浏览器之间的Javascript差异(秒)

时间:2022-07-06 17:07:16

I have a function where i need to calculate the difference in seconds between two datetime. I do something like this:

我有一个函数,我需要计算两个日期时间之间的秒差。我做这样的事情:

var sData = new Date(Date.parse(sData));
var tData = new Date(Date.parse(response.scroll.datain));
var timeDiff = Math.abs(tData.getTime() - sData.getTime());
timeDiff = parseInt(timeDiff)/1000;

Chrome return the correct difference in seconds between the two datetime fields, firefox and opera return 0. How can achieve this result in cross browser mode?

Chrome返回两个日期时间字段之间的正确差异,firefox和opera返回0.如何在跨浏览器模式下实现此结果?

Thanks in advance

提前致谢

1 个解决方案

#1


0  

the issue is MYSQL date-time format. A workaround could be this:

问题是MYSQL日期时间格式。解决方法可能是这样的:

var t = response.scroll[i].datain.split(/[- :]/);
var s = sData.split(/[- :]/);

// Apply each element to the Date function
var tData = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
var sData = new Date(s[0], s[1]-1, s[2], s[3], s[4], s[5]);

work for all major browser.

适用于所有主流浏览器。

#1


0  

the issue is MYSQL date-time format. A workaround could be this:

问题是MYSQL日期时间格式。解决方法可能是这样的:

var t = response.scroll[i].datain.split(/[- :]/);
var s = sData.split(/[- :]/);

// Apply each element to the Date function
var tData = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
var sData = new Date(s[0], s[1]-1, s[2], s[3], s[4], s[5]);

work for all major browser.

适用于所有主流浏览器。