2

How do I create a local time(time zone -> UTC + 3:30) that would show seconds with jQuery?
I do not want to use a plugin, and prefer a short code.

I do not use the following example because it gets the system time and is in javascript. It is only an example of what I want.
EXAMPLE of javascript: http://jsfiddle.net/HZmPg/

EDIT: What is your opinion about this code?

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

If this can not be done with jQuery, what is the solution?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Sheikhasa Mozali
  • 237
  • 1
  • 9
  • 16
  • 2
    jquery really wasn't designed for data manipulation "jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions". What are you trying to do? When you say local time do you mean server time? The example you have a fiddle of gets the local machine time what more do you want? – scrappedcola Aug 11 '11 at 19:33
  • Please edit your post and add the code inside your question. – plaes Aug 11 '11 at 19:37
  • http://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object – zod Aug 11 '11 at 19:38
  • @ plaes - i edit post please see and tell me about it? – Sheikhasa Mozali Aug 11 '11 at 19:49
  • why not keep the time as UTC and then do whatever you want with it? Much easier to keep track of. – Matt Whipple Oct 19 '12 at 02:38

1 Answers1

0

You could make this a function with a callback

function getTime(timezone, callback) {
        $.getJSON("http://json-time.appspot.com/time.json", {
            tz: timezone,
            callback: '?'
        },
        callback);
    });
}

$(document).ready(function(){
    var timezone = "Europe/Berlin",
        hello = function (time) {
            if (time.hour < 12) {
                alert ("Good morning in "+ timezone);
            } else {
                alert ("Good afternoon in "+ timezone);
            }
        };
    getTime(timezone, hello);
}

Or you could use deferred objects an promises to clean things up:

function getTime(timezone) {
    return $.getJSON("http://json-time.appspot.com/time.json?tz="+timezone+"&callback=?").promise();
}


$(document).ready(function(){
    var timezone = "Europe/Berlin",
        hello = function (time) {
            if (time.hour < 12) {
                alert ("Good morning in "+ timezone);
            } else {
                alert ("Good afternoon in "+ timezone);
            }
        };
    getTime(timezone).done(hello);
});
TxRegex
  • 2,347
  • 21
  • 20