我有以下javascript代码,用于评估两个日期之间的时间跨度,如下所示:
jQuery(document).ready(
function () {
setInterval(function () {
var date1 = new Date();
var date2 = new Date("30/Feb/2017 07:07:07");
var diff = Math.abs(date2.getTime() - date1.getTime());
var days = Math.floor(diff / (1000 * 60 * 60 * 24));
diff -= days * (1000 * 60 * 60 * 24);
if (days < 10)
days = "0" + days;
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);
if (hours < 10)
hours = "0" + hours;
var mins = Math.floor(diff / (1000 * 60));
diff -= mins * (1000 * 60);
if (mins < 10)
mins = "0" + mins;
var seconds = Math.floor(diff / (1000));
if (seconds < 10)
seconds = "0" + seconds;
diff -= seconds * (1000);
var time = days + " روز, " + hours + " ساعت, " + mins + " دقیقه, " + seconds + " ثانیه";
document.getElementById("remaingTimeDays").textContent = days;
document.getElementById("remaingTimeHours").textContent = hours;
document.getElementById("remaingTimeMinutes").textContent = mins;
document.getElementById("remaingTimeSeconds").textContent = seconds;
}, 1000);
});
此代码在Firefox和Chrome中运行良好,但在Safari,IE和Edge中无效。 我几乎阅读了有关该主题的所有问题,但我找不到任何合适的解决方案。
答案 0 :(得分:0)
问题是我以错误的格式写出截止日期。 正确的格式如下:
var date2 = new Date("02/30/2017 07:07:07");
02:是月 30:是一天 2017年:等等。感谢我的朋友帮助纠正这个问题。非常感谢所有人