0

I need to dictate what time it is. It seems that .getHours uses the users system time. What if I need to set this to a certain timezone? I can't find anything in the documentation on setting the default timezone.

I have a delivery script for a site in Chicago, and currently if someone orders on the west coast they can inadvertently game the system and the 12 noon delivery cut off for next day delivery.

EDIT: So I found this:

$(document).ready(function(){
  var timezone = "Europe/Berlin";
  $.getJSON("http://json-time.appspot.com/time.json?tz="+timezone+"&callback=?",
    function(data){
      if (data.hour < 12) {
        alert ("Good morning in "+timezone);
      } else {
        alert ("Good afternoon in "+timezone);
      }
    })
});

from here: How can I obtain the local time in jQuery?

Not sure where to take this with the time validation though....

Community
  • 1
  • 1
Jamie
  • 373
  • 1
  • 4
  • 16

1 Answers1

2

As JavaScript is client-side, it makes sense that it would get the data from the user's system.

If you want to manage the time, you'll have to (ultimately) do so server-side (which is the case for any user input, really). While you can provide feedback on the client, you should always validate inputs server-side to ensure the user isn't trying to get around any of your validation. Client-side validation is purely for convenience and user feedback.

You can start the user with a certain time by sending data from the server to populate the jQuery datepicker's time. This way, you'll at least start out with a certain time the pertains to the timezone you are interested in. Again, the user doesn't have to obey that as they control the client and, thus, could always game the system with out server-side checks.

JasCav
  • 34,458
  • 20
  • 113
  • 170
  • Thanks for the input, that's what I thought as well. :/ Unfortunately the server has a very aggressive cache system and the time will be off by as much as a day. On top of that most assets are over a CDN. I guess I will remain stumped on this. Wishing I could grab time from an external time service somewhere. – Jamie Mar 19 '11 at 13:19
  • So I was looking at time servers. If there any way that you know of to get the time from something like time.gov via datepicker? – Jamie Mar 19 '11 at 13:36
  • This is another one from yahoo: http://developer.yahoo.com/util/timeservice/V1/getTime.html I am just unclear as to how to use this with datepicker to set the time. – Jamie Mar 19 '11 at 13:39
  • 1
    Aha! An even better one: http://json-time.appspot.com/time.json?tz=America/Chicago – Jamie Mar 19 '11 at 13:49