0

I have this code working but it reads the current time of their computer, I want to get the hours from USA only! doesn't matter if you access it from another country, the css will be used according to USA time.

 <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>
Procomp Pcm
  • 29
  • 2
  • 6

1 Answers1

2

Use Date.getTimezoneOffset() to get the difference between the user time zone and UTC time. Then, if you know the difference between UTC time and the time zone you need:

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();
Oscar Paz
  • 18,084
  • 3
  • 27
  • 42