如何在Jquery中检查当前日期和时间

时间:2022-04-10 21:53:56

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

只是想知道是否有办法检查Jquery中的当前日期和时间。

3 个解决方案

#1


21  

In JavaScript you can get the current date and time using the Date object;

在JavaScript中,您可以使用Date对象获取当前日期和时间;

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:

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.

创建一个简单的JSON字符串,使用当前服务器时间初始化一个新的Date对象,该值乘以1000,因为PHP以秒为单位处理时间戳,JavaScript以毫秒为单位。

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.

只需发出getJSON请求即可从time.php中检索日期,并访问返回对象的serverTime属性。

#2


6  

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.

您可以使用jQuery向页面发出Ajax请求,然后返回当前日期和时间。

#3


2  

To clarify the answers provided so far...

澄清到目前为止提供的答案......

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

    获取当前日期并不是使用jquery所必需的(即使它有能力这样做),因为原始javascript提供了这种功能。

    var currentTimeAndDate = new Date();

    var currentTimeAndDate = new Date();

  2. 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).

静态地是服务器直接将时间/日期呈现到页面中 - 例如,您可能想要“此页面是在格林尼治标准时间1962年7月24日12:34生成的”(这将是一个巧妙的技巧,考虑到网络服务器在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

动态地是当客户端页面通过使用AJAX调用主动更新时间显示以询问服务器的时间 - 这是@CMS提供的响应中描述的方法

#1


21  

In JavaScript you can get the current date and time using the Date object;

在JavaScript中,您可以使用Date对象获取当前日期和时间;

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:

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.

创建一个简单的JSON字符串,使用当前服务器时间初始化一个新的Date对象,该值乘以1000,因为PHP以秒为单位处理时间戳,JavaScript以毫秒为单位。

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.

只需发出getJSON请求即可从time.php中检索日期,并访问返回对象的serverTime属性。

#2


6  

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.

您可以使用jQuery向页面发出Ajax请求,然后返回当前日期和时间。

#3


2  

To clarify the answers provided so far...

澄清到目前为止提供的答案......

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

    获取当前日期并不是使用jquery所必需的(即使它有能力这样做),因为原始javascript提供了这种功能。

    var currentTimeAndDate = new Date();

    var currentTimeAndDate = new Date();

  2. 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).

静态地是服务器直接将时间/日期呈现到页面中 - 例如,您可能想要“此页面是在格林尼治标准时间1962年7月24日12:34生成的”(这将是一个巧妙的技巧,考虑到网络服务器在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

动态地是当客户端页面通过使用AJAX调用主动更新时间显示以询问服务器的时间 - 这是@CMS提供的响应中描述的方法