javascript - css 取决于美国当前时间

标签 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/

上一篇:html - 文本输入内的格式化/固定尺寸

下一篇:css - 如何忽略空跨度

相关文章:

Javascript-将点击事件添加到动态生成的表格单元格

jquery - Bootstrap 3 向导?

javascript - 我想要一个垂直(反馈)按钮在鼠标悬停时有一个轻微的动画滑出效果

javascript - 匿名函数错误 : SyntaxError: Unexpected end of input

javascript - angularJS $scope 更改不更新 View

javascript - JQuery 查找 span 样式颜色以替换为颜色哈希

javascript - 向文本溢出 : ellipsis 的元素添加标题属性

javascript - 在 HTML 中使用 Canvas 处理 Sprite 加载

javascript - 仅在 HTTP 中支持跨域请求 - 访问 Sencha Touch 示例

jQuery 使用两个按钮在三种不同样式之间更改单个 div 的类