javascripts/jquery 倒计时在手机上给出错误的时间
javascripts/jquery countdown giving wrong hours on mobile
我正在使用 jquery countdown.js 为日食倒计时。在我的桌面上一切都正确。在移动设备 (android) 上,倒计时提前一小时。在别人的手机上,倒计时显示为0小时。我究竟做错了什么?我不考虑时区吗?那应该重要吗?日食倒计时(从现在开始不到 4 小时),所以我真的可以快速使用帮助!
var date = new Date("2017-08-21T13:18:00"); //1:18 p.m. EST
$("#dayElem")
.countdown(date, function(event) {
if (event.strftime('%-D') < 10) {
$(this).text('0' +
event.strftime('%-D')
);
} else {
$(this).text(
event.strftime('%-D')
);
}
});
$("#hourElem")
.countdown(date, function(event) {
if (event.strftime('%-H') < 10) {
$(this).text('0' +
event.strftime('%-H')
);
} else {
$(this).text(
event.strftime('%-H')
);
}
});
$("#minuteElem")
.countdown(date, function(event) {
if (event.strftime('%-M') < 10) {
$(this).text('0' +
event.strftime('%-M')
);
} else {
$(this).text(
event.strftime('%-M')
);
}
});
$("#secondElem")
.countdown(date, function(event) {
if (event.strftime('%-S') < 10) {
$(this).text('0' +
event.strftime('%-S')
);
} else {
$(this).text(
event.strftime('%-S')
);
}
});
我认为你的假设是正确的。您必须考虑当前时区。
完成此操作的最简单方法是使用 moment.js (https://momentjs.com/) 并像这样修改代码:
var date = moment.utc('2017-08-21T17:18:00').toDate();
$("#dayElem")
.countdown(date, function (event) {
if (event.strftime('%-D') < 10) {
$(this).text('0' +
event.strftime('%-D')
);
} else {
$(this).text(
event.strftime('%-D')
);
}
});
$("#hourElem")
.countdown(date, function (event) {
if (event.strftime('%-H') < 10) {
$(this).text('0' +
event.strftime('%-H')
);
} else {
$(this).text(
event.strftime('%-H')
);
}
});
$("#minuteElem")
.countdown(date, function (event) {
if (event.strftime('%-M') < 10) {
$(this).text('0' +
event.strftime('%-M')
);
} else {
$(this).text(
event.strftime('%-M')
);
}
});
$("#secondElem")
.countdown(date, function (event) {
if (event.strftime('%-S') < 10) {
$(this).text('0' +
event.strftime('%-S')
);
} else {
$(this).text(
event.strftime('%-S')
);
}
});
另请注意,我指定的时间是 UTC 而不是 EST。
当 运行 你的代码在我的时区 CET 时,我只得到零。使用上面的代码时,我在发布此代码时花了大约 3 小时 10 分钟。
我正在使用 jquery countdown.js 为日食倒计时。在我的桌面上一切都正确。在移动设备 (android) 上,倒计时提前一小时。在别人的手机上,倒计时显示为0小时。我究竟做错了什么?我不考虑时区吗?那应该重要吗?日食倒计时(从现在开始不到 4 小时),所以我真的可以快速使用帮助!
var date = new Date("2017-08-21T13:18:00"); //1:18 p.m. EST
$("#dayElem")
.countdown(date, function(event) {
if (event.strftime('%-D') < 10) {
$(this).text('0' +
event.strftime('%-D')
);
} else {
$(this).text(
event.strftime('%-D')
);
}
});
$("#hourElem")
.countdown(date, function(event) {
if (event.strftime('%-H') < 10) {
$(this).text('0' +
event.strftime('%-H')
);
} else {
$(this).text(
event.strftime('%-H')
);
}
});
$("#minuteElem")
.countdown(date, function(event) {
if (event.strftime('%-M') < 10) {
$(this).text('0' +
event.strftime('%-M')
);
} else {
$(this).text(
event.strftime('%-M')
);
}
});
$("#secondElem")
.countdown(date, function(event) {
if (event.strftime('%-S') < 10) {
$(this).text('0' +
event.strftime('%-S')
);
} else {
$(this).text(
event.strftime('%-S')
);
}
});
我认为你的假设是正确的。您必须考虑当前时区。 完成此操作的最简单方法是使用 moment.js (https://momentjs.com/) 并像这样修改代码:
var date = moment.utc('2017-08-21T17:18:00').toDate();
$("#dayElem")
.countdown(date, function (event) {
if (event.strftime('%-D') < 10) {
$(this).text('0' +
event.strftime('%-D')
);
} else {
$(this).text(
event.strftime('%-D')
);
}
});
$("#hourElem")
.countdown(date, function (event) {
if (event.strftime('%-H') < 10) {
$(this).text('0' +
event.strftime('%-H')
);
} else {
$(this).text(
event.strftime('%-H')
);
}
});
$("#minuteElem")
.countdown(date, function (event) {
if (event.strftime('%-M') < 10) {
$(this).text('0' +
event.strftime('%-M')
);
} else {
$(this).text(
event.strftime('%-M')
);
}
});
$("#secondElem")
.countdown(date, function (event) {
if (event.strftime('%-S') < 10) {
$(this).text('0' +
event.strftime('%-S')
);
} else {
$(this).text(
event.strftime('%-S')
);
}
});
另请注意,我指定的时间是 UTC 而不是 EST。
当 运行 你的代码在我的时区 CET 时,我只得到零。使用上面的代码时,我在发布此代码时花了大约 3 小时 10 分钟。