Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check for current date and time in Jquery

Tags:

jquery

date

time

Just wondering if there was a way to check the current date and time in Jquery.

like image 582
akano1 Avatar asked Aug 11 '09 07:08

akano1


People also ask

How can I get current Date in jquery DD MMM YYYY format?

Your answer get current date in jquery in dd/mm/yyyy format” Code Answer. var mm = String(today. getMonth() + 1).

What is Date now () in JavaScript?

The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

How can get current Date in textbox using jquery?

datepicker. formatDate("dd-mm-yy", new Date())); $("#return_on").


1 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.

like image 109
Christian C. Salvadó Avatar answered Oct 01 '22 11:10

Christian C. Salvadó