是否可以创建一个jQuery函数来获取当前日期和时间?我一直在寻找文档,但到目前为止还没有找到任何东西……
发布于 2009-03-22 00:19:44
@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();
因此,如果您希望将其显示为mm/dd/yyyy,您可以这样做:
var displayDate = (myDate.getMonth()+1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
查看Date对象的full reference。不幸的是,打印出各种格式并不像使用其他服务器端语言那样好。出于这个原因,there-are-many-functions可以在野外使用。
发布于 2012-07-04 20:45:28
是,有可能:
jQuery.now()
或者简单地说
$.now()
see jQuery Documentation for jQuery.now()
发布于 2009-03-22 00:21:24
你不需要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>
您可以在此处查看完整的参考资料:http://www.hunlock.com/blogs/Javascript_Dates-The_Complete_Reference
https://stackoverflow.com/questions/670290
复制相似问题