Just wondering if there was a way to check the current date and time in Jquery.
3 Answers
In JavaScript you can get the current date and time using the Date object;
var now = new Date();
This will get the local client machine time, if you want to do timezone adjustments or get the time from a server, check my answer to this question.
Edit: In response to your comment, you can retrieve your server time to the client side very easily:
time.php:
<? echo '{serverTime: new Date(' . time()*1000 .')}';?>
A simple JSON string is created, with a new Date object initialized with the current server time, this value is multiplied by 1000, because PHP handles timestamps in seconds, and JavaScript does it in milliseconds.
And on your other you can get the server time like this:
$.getJSON('time.php', function (data) {
alert(data.serverTime);
});
Just make a getJSON request to retrieve the date from time.php, and access to the serverTime property of the returned object.

- 1
- 1

- 807,428
- 183
- 922
- 838
-
Is it possible to check the date on the server side using php and assign it to a variable and then use that variable in Jquery? – akano1 Aug 11 '09 at 07:11
-
Thanks for the answer,very helpfull, just wondering if there is a way to check for a certain day in the week (i.e friday) thanks – akano1 Aug 11 '09 at 07:32
-
In PHP give a look to the strtotime function http://is.gd/2budP... i.e.: strtotime("next Friday") – Christian C. Salvadó Aug 11 '09 at 07:40
-
i am sorry to ask this, but I am curious.. is this what is called JSONP that you are getting from time.php, or is it just plain pure JSON? – sharat87 Aug 11 '09 at 13:46
-
@sharat87: Its a plain JSON response. – Christian C. Salvadó Aug 11 '09 at 15:06
Its not a god practice to get the current date and time from the client machine.
Make this check a server side one.
You can use jQuery to make an Ajax request to a page and then return the current date and time.

- 184,426
- 49
- 232
- 263
-
unless you know your users timezone (eg: theyy're logged in and have provided it at some stage), your server's time is unlikely to be their local time. In this case, isn't it better to use the time provided by the users browser? – drevicko May 25 '13 at 10:10
To clarify the answers provided so far...
Obtaining the current date is not something that's necessary to use jquery for (Even if it had the ability to do so), as raw javascript provides this facility.
var currentTimeAndDate = new Date();
With very few exceptions, you should avoid obtaining the current time/date from the client machine, as you have no way of knowing if their clock is set correctly. Always get the time date from the server, to provide some level of predictability. This, however, is not foolproof either, as the client could genuinely be one day ahead of, or behind, the server, so you'll have to decide how to handle the situation in a way that suits your application.
UPDATE:
There is also a need to distinguish between obtaining the date/time statically and dynamically.
Statically is when the server renders the time/date into the page directly - for example, you may want to have a 'This page was generated at 12:34 GMT on July 24th 1962' (Which would be a neat trick, considering that web servers didn't exist in 1962).
Dynamically is when the client page actively updated the time display by using AJAX calls to ask the server for the curent time - this is the method being described in the response provided by @CMS

- 4,302
- 22
- 22