停止将Javascript DateTime调整为本地时区 [英] Stop Javascript DateTime adjusting to local time zone
问题描述
我收到了我在javascript的html页面中使用的正确的Unix时间。但是,当我查看或提醒unixtime时,发生的是基于桌面的当前时区调整了日期,但是我需要DateTime不含时区。
I am receiving the correct unix time that I use in my html page in my javascript. However, when I view or alert the unixtime, what happens is that the date is adjusted based on my desktop's current timezone, but I need the DateTime to be without the timezone.
这就是我在JavaScript中传递数据的方式,这是我使用unixtime的多种方式之一:
This is how I pass the data in my javascript, one of the many ways I use the 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时间应显示为:
what the unix time should show is:
2015年6月13日13:57(如果已转换)
Jun 13 2015 13:57 (if it was converted)
,但显示此日期:
2015年6月13日21:57马来半岛时间
Jun 13 2015 21:57 malay peninsula time
基本上发生的是,javascript获取了我的本地时区并将该日期转换为该时区。我该如何强制它(以及我使用的所有其他JavaScript函数)使用它应该显示的原始时间?
What basically happens is that the javascript gets my local time zone and converts that date to that time zone. How could I force it(and all other javascript functions that I use) to use the original time it supposed to be showing?
推荐答案
如果要使用UTC格式,则可以使用许多UTC方法之一。例如,您可以使用 Date.prototype.toUTCString
:
If you want the UTC format you can use one of the many UTC methods. For instance you could use Date.prototype.toUTCString
:
var unixTS = 1434203820 * 1000; // JS date is in millseconds
var date = new Date(unixTS);
document.write(date.toUTCString());
这篇关于停止将Javascript DateTime调整为本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!