问题
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...
回答1:
@nickf's correct. However, to be a little more precise:
// 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:
var displayDate = (myDate.getMonth()+1) + '/' + (myDate.getDate()) + '/' + myDate.getFullYear();
Check out the full reference of 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-functions available in the wild.
回答2:
Yes, it is possible:
jQuery.now()
or simply
$.now()
see jQuery Documentation for jQuery.now()
回答3:
You don't need jquery to do that, just javascript. For example, you can do a timer using this:
<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
回答4:
It's plain javascript:
new Date()
回答5:
Digital Clock with jQuery
<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>
回答6:
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 :)
回答7:
Annoyingly Javascript's date.getSeconds()
et al will not pad the result with zeros 11:0:0
instead of 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)
Documentation
来源:https://stackoverflow.com/questions/670290/jquery-gettime-function