2014-03-13 68 views
0

我有此代码工作,但它读取他们的计算机的当前时间,我只想从美国获得小时!不要紧,如果你从另一个国家访问它,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> 
+1

美国有多个时区。你瞄准哪一个? –

+1

相关:http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript – Douglas

回答

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