<分区>
标签 javascript jquery css
<分区>
我可以使用这段代码,但它会读取他们计算机的当前时间,我只想获取美国的时间!如果您从其他国家/地区访问它也没关系,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>
最佳答案
使用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();
关于javascript - css 取决于美国当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392481/
相关文章:
javascript - 我想要一个垂直(反馈)按钮在鼠标悬停时有一个轻微的动画滑出效果
javascript - 匿名函数错误 : SyntaxError: Unexpected end of input
javascript - angularJS $scope 更改不更新 View
javascript - JQuery 查找 span 样式颜色以替换为颜色哈希
javascript - 向文本溢出 : ellipsis 的元素添加标题属性
javascript - 在 HTML 中使用 Canvas 处理 Sprite 加载