时间jquery
jQuery 时间操作
jQuery 本身没有专门的时间处理函数,但可以结合 JavaScript 的 Date 对象和 jQuery 的选择器、事件处理等功能来操作时间。
获取当前时间
var currentTime = new Date();
console.log(currentTime);
格式化时间

var formattedTime = $.format.date(currentTime, 'yyyy-MM-dd HH:mm:ss');
console.log(formattedTime);
延迟执行
setTimeout(function() {
alert('时间到了!');
}, 3000); // 3秒后执行
定时重复执行

var intervalId = setInterval(function() {
console.log(new Date());
}, 1000); // 每秒执行一次
// 停止定时器
$('#stopButton').click(function() {
clearInterval(intervalId);
});
时间选择器插件 推荐使用 jQuery UI 的 Datepicker 或其他时间选择插件:
$('#datepicker').datepicker();
计算时间差
var startTime = new Date('2023-01-01');
var endTime = new Date();
var diff = endTime - startTime; // 毫秒数
var diffDays = Math.floor(diff / (1000 * 60 * 60 * 24));
console.log('相差天数:' + diffDays);
注意事项
- 时区处理需要考虑
- 日期格式在不同浏览器中可能有差异
- 对于复杂的时间操作建议使用 Moment.js 等专门的时间库






