Stop Javascript DateTime adjusting to local time zone

血红的双手。 提交于 2019-12-25 16:01:11

问题


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.

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>

what the unix time should show is:

Jun 13 2015 13:57 (if it was converted)

but it is showing this date:

Jun 13 2015 21:57 malay peninsula time

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?


回答1:


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());



回答2:


Maybe what you want is converting back the timestamp to UTC format ?

How do you convert a Javascript timestamp into UTC format?



来源:https://stackoverflow.com/questions/31127544/stop-javascript-datetime-adjusting-to-local-time-zone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!