停止 Javascript DateTime 调整到本地时区
Stop Javascript DateTime adjusting to local time zone
我在 javascript 的 html 页面中收到正确的 unix 时间。但是,当我查看或提醒 unixtime 时,发生的情况是日期根据我桌面的当前时区进行了调整,但我需要 DateTime 没有时区。
这就是我在 javascript 中传递数据的方式,这是我使用 unixtime 的众多方式之一:
<script type="text/javascript">
$(document).ready(function(){
var a = <?php echo "1434203820"; // an example only?>;
var date = new Date(a);
alert(date);
});
</script>
unix时间应该显示的是:
Jun 13 2015 13:57 (if it was converted)
但它显示的是这个日期:
Jun 13 2015 21:57 malay peninsula time
基本上发生的是 javascript 获取我的本地时区并将该日期转换为该时区。我怎么能强制它(以及我使用的所有其他 javascript 函数)使用它应该显示的原始时间?
也许您想要将时间戳转换回 UTC 格式?
How do you convert a Javascript timestamp into UTC format?
如果您想要 UTC 格式,您可以使用许多 UTC 方法中的一种。例如,您可以使用 Date.prototype.toUTCString
:
var unixTS = 1434203820 * 1000; // JS date is in millseconds
var date = new Date(unixTS);
document.write(date.toUTCString());
我在 javascript 的 html 页面中收到正确的 unix 时间。但是,当我查看或提醒 unixtime 时,发生的情况是日期根据我桌面的当前时区进行了调整,但我需要 DateTime 没有时区。
这就是我在 javascript 中传递数据的方式,这是我使用 unixtime 的众多方式之一:
<script type="text/javascript">
$(document).ready(function(){
var a = <?php echo "1434203820"; // an example only?>;
var date = new Date(a);
alert(date);
});
</script>
unix时间应该显示的是:
Jun 13 2015 13:57 (if it was converted)
但它显示的是这个日期:
Jun 13 2015 21:57 malay peninsula time
基本上发生的是 javascript 获取我的本地时区并将该日期转换为该时区。我怎么能强制它(以及我使用的所有其他 javascript 函数)使用它应该显示的原始时间?
也许您想要将时间戳转换回 UTC 格式?
How do you convert a Javascript timestamp into UTC format?
如果您想要 UTC 格式,您可以使用许多 UTC 方法中的一种。例如,您可以使用 Date.prototype.toUTCString
:
var unixTS = 1434203820 * 1000; // JS date is in millseconds
var date = new Date(unixTS);
document.write(date.toUTCString());