如何在 Jquery 中检查当前日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1258805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check for current date and time in Jquery
提问by akano1
Just wondering if there was a way to check the current date and time in Jquery.
只是想知道是否有办法检查 Jquery 中的当前日期和时间。
回答by CMS
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:
时间.php:
<? echo '{serverTime: new Date(' . time()*1000 .')}';?>
A simple JSONstring 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 getJSONrequest to retrieve the date from time.php, and access to the serverTime property of the returned object.
只需发出一个getJSON请求即可从time.php检索日期,并访问返回对象的 serverTime 属性。
回答by rahul
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 请求,然后返回当前日期和时间。
回答by belugabob
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.
获取当前日期不是使用 jquery 所必需的(即使它有能力这样做),因为原始 javascript 提供了这个功能。
var currentTimeAndDate = new Date();
除了极少数例外,您应该避免从客户端计算机获取当前时间/日期,因为您无法知道他们的时钟是否设置正确。始终从服务器获取时间日期,以提供某种程度的可预测性。然而,这也不是万无一失的,因为客户端可能真正领先或落后于服务器一天,因此您必须决定如何以适合您的应用程序的方式处理这种情况。
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 生成的”(考虑到 Web服务器在 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 提供的响应中描述的方法