jQuery getTime 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/670290/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:33:19  来源:igfitidea点击:

jQuery getTime function

jquerygettime

提问by Tsundoku

is it possible to create a jQuery function so that it gets current date and time? I've been looking around documentation but haven't found anything so far...

是否可以创建一个 jQuery 函数来获取当前日期和时间?我一直在寻找文档,但到目前为止还没有找到任何东西......

回答by Paolo Bergantino

@nickf's correct. However, to be a little more precise:

@nickf 是对的。但是,更准确地说:

// if you try to print it, it will return something like:
// Sat Mar 21 2009 20:13:07 GMT-0400 (Eastern Daylight Time)
// This time comes from the user's machine.
var myDate = new Date();

So if you want to display it as mm/dd/yyyy, you would do this:

所以如果你想把它显示为 mm/dd/yyyy,你可以这样做:

var displayDate = (myDate.getMonth()+1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();

Check out the full referenceof the Date object. Unfortunately it is not nearly as nice to print out various formats as it is with other server-side languages. For this reason there-are-many-functionsavailable in the wild.

查看Date 对象的完整参考。不幸的是,打印出各种格式并不像使用其他服务器端语言那样好。出于这个原因---功能在野外可用。

回答by Alex Doe

Yes, it is possible:

是的,这是可能的:

jQuery.now()

or simply

或者干脆

$.now()

see jQuery Documentation for jQuery.now()

请参阅 jQuery.now() 的 jQuery 文档

回答by eKek0

You don't need jquery to do that, just javascript. For example, you can do a timer using this:

你不需要 jquery 来做到这一点,只需要 javascript。例如,您可以使用以下方法执行计时器:

<body onload="clock();">

<script type="text/javascript">
function clock() {
   var now = new Date();
   var outStr = now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
   document.getElementById('clockDiv').innerHTML=outStr;
   setTimeout('clock()',1000);
}
clock();
</script>   

<div id="clockDiv"></div>

</body>

You can view a complete reference here: http://www.hunlock.com/blogs/Javascript_Dates-The_Complete_Reference

您可以在此处查看完整的参考:http: //www.hunlock.com/blogs/Javascript_Dates-The_Complete_Reference

回答by nickf

It's plain javascript:

这是普通的javascript:

new Date()

回答by Dharam Mali

Digital Clock with jQuery

带有 jQ​​uery 的数字时钟

  <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
  <script type="text/javascript">
  $(document).ready(function() {
  function myDate(){
  var now = new Date();

  var outHour = now.getHours();
  if (outHour >12){newHour = outHour-12;outHour = newHour;}
  if(outHour<10){document.getElementById('HourDiv').innerHTML="0"+outHour;}
  else{document.getElementById('HourDiv').innerHTML=outHour;}

  var outMin = now.getMinutes();
  if(outMin<10){document.getElementById('MinutDiv').innerHTML="0"+outMin;}
  else{document.getElementById('MinutDiv').innerHTML=outMin;}

  var outSec = now.getSeconds();
  if(outSec<10){document.getElementById('SecDiv').innerHTML="0"+outSec;}
  else{document.getElementById('SecDiv').innerHTML=outSec;}

} myDate(); setInterval(function(){ myDate();}, 1000); }); </script> <style> body {font-family:"Comic Sans MS", cursive;} h1 {text-align:center;background: gray;color:#fff;padding:5px;padding-bottom:10px;} #Content {margin:0 auto;border:solid 1px gray;width:140px;display:table;background:gray;} #HourDiv, #MinutDiv, #SecDiv {float:left;color:#fff;width:40px;text-align:center;font-size:25px;} span {float:left;color:#fff;font-size:25px;} </style> <div id="clockDiv"></div> <h1>My jQery Clock</h1> <div id="Content"> <div id="HourDiv"></div><span>:</span><div id="MinutDiv"></div><span>:</span><div id="SecDiv"></div> </div>

回答by Anerty

this is my way :

这是我的方式 :

    <script type="text/javascript">
$(document).ready(function() {
    setInterval(function(){currentTime("#idTimeField")}, 500);
});
function currentTime(field) {
    var now = new Date();
    now = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
    $(field).val(now);
}

it's not maybe the best but do the work :)

这可能不是最好的,但要做好工作:)

回答by Martin Lyne

Annoyingly Javascript's date.getSeconds()et alwill not pad the result with zeros 11:0:0instead of 11:00:00.

令人讨厌的是,Javascriptdate.getSeconds()等人不会用零11:0:0代替11:00:00.

So I like to use

所以我喜欢用

date.toLocaleTimestring()

Which renders 11:00:00 AM. Just beware when using the extra options, some browsers don't support them (Safari)

其中呈现11:00:00 AM. 使用额外选项时请注意,某些浏览器不支持它们(Safari)

Documentation

文档