使用jquery处理日期时间
2013-04-01 17:16:18 阿炯

其实jquery本身对日期时间处理的支持并不好,究其原因,是因为javascript本身对日期时间支持就很好了。
初始化一个日期对象:
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();

常见的示例:
<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>

jquery从1.4.3版本开始,支持内置式的日期时间操作。参考地址:http://api.jquery.com/jQuery.now/
jQuery.now()
or simply
$.now()

The $.now() method is a shorthand for the number returned by the expression (new Date).getTime().

它的返回值为当前的unix时间戳,对日期时间提供有限的支持。
示例代码:
<script type="text/javascript">
function int(){
var time = jQuery.now();//jQuery.now() shows the current timestamp
$('#clockDiv').text(time);
}
setInterval(int,1000);// this setInterval() method call int function after each 1 second
</script>

$(document).ready(function(){
 $("#clockDiv").html($.now());
});

如果想在jquery中操作日期时间像使用'Date'对象那样,建议使用'jquery.datepick'组件。
在引入了'jquery.datepick.js'组件后,可以很方便地设置各种日期时间格式。
$('#cday').datepick({dateFormat: 'yyyy-mm-dd'});

$.datepicker.formatDate('yy/mm/dd', new Date());

$( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());