我有这个代码工作,但它读取了他们的计算机的当前时间,我想从美国只获得时间!如果你从其他国家访问它并不重要,css将根据美国时间使用。
<script type="text/javascript">
datetoday = new Date(); // create new Date()
timenow = datetoday.getTime(); // grabbing the time, it is now
datetoday.setTime(timenow); // setting the time now to datetoday variable
hournow = datetoday.getHours(); //the hour it is
<script type="text/javascript">
$(document).ready(function () {
datetoday = new Date(); // create new Date()
timenow = datetoday.getTime(); // grabbing the time it is now
datetoday.setTime(timenow); // setting the time now to datetoday variable
hournow = datetoday.getHours(); //the hour it is
if (hournow >= 18) // if it is after 6pm
$('body').addClass('evening');
else if (hournow >= 12) // if it is after 12pm
$('body').addClass('afternoon');
else if (hournow >= 6) // if it is after 6am
$('body').addClass('morning');
else if (hournow >= 0) // if it is after midnight
$('body').addClass('midnight');
});
</script>
答案 0 :(得分:2)
使用Date.getTimezoneOffset()
获取用户时区和UTC时间之间的差异。
然后,如果您知道UTC时间与所需时区之间的差异:
var TIME_OFFSET = 5; //five hours difference to US East time.
var date = new Date();
date.setMinutes(date.getHours() + (date.getTimezoneOffset() / 60) - TIME_OFFSET);
var usHour = date.getHours();