我遇到了同样的问题。三星智能电视上的new Date() 不会根据当前设置获取时间,而是获取底层系统时间。它也总是返回一个 UTC 时间戳。
幸运的是,有一个 API 函数 GetEpochTime() 可以让您(在某种程度上)修复它。
这是我在三星平台上使用的代码:
(function (OldDate) {
// Get a reference to Samsung's TIME API
var time = document.createElement('object');
time.setAttribute('classid', 'clsid:SAMSUNG-INFOLINK-TIME');
document.body.appendChild(time);
// Replace the existing Date() constructor
window.Date = function (a, b, c, d, e, f, g) {
switch (arguments.length) {
case 0:
return new OldDate(time.GetEpochTime() * 1000);
case 1: return new OldDate(a);
case 2: return new OldDate(a, b);
case 3: return new OldDate(a, b, c);
case 4: return new OldDate(a, b, c, d);
case 5: return new OldDate(a, b, c, d, e);
case 6: return new OldDate(a, b, c, d, e, f);
case 7: return new OldDate(a, b, c, d, e, f, g);
}
};
// Copy static function properties
Date.now = function () { return time.GetEpochTime() * 1000; };
Date.UTC = OldDate.UTC;
Date.parse = OldDate.parse;
})(Date);
new Date() 现在可以很好地在我的 2013 三星智能电视上显示当前时间。但是,在将时间戳与当前时间进行比较时,您可能仍然会遇到问题。