jquery里的时间函数
在网页开发中,经常需要使用时间函数来处理时间相关的操作。Jquery作为一个广泛使用的JavaScript库,也提供了一些非常方便的时间函数。
其中,最常用的时间函数包括:
// 获取当前时间var now = new Date();// 获取当前时间戳(时间戳是指从 1970 年 1 月 1 日 00:00:00 UTC 到最近一次活动时间之间的毫秒数)var timestamp = now.getTime();// 格式化时间var format = now.format("yyyy-MM-dd hh:mm:ss");
我们可以根据自己需要定制时间的格式,比如:
// 格式化时间Date.prototype.format = function (format) {var o = {"M ": this.getMonth() 1, // month"d ": this.getDate(), // day"h ": this.getHours(), // hour"m ": this.getMinutes(), // minute"s ": this.getSeconds(), // second"q ": Math.floor((this.getMonth() 3) / 3), // quarter"S": this.getMilliseconds() // millisecond}if (/(y )/.test(format)) {format = format.replace(RegExp.$1, (this.getFullYear() "").substr(4 - RegExp.$1.length));}for (var k in o) {if (new RegExp("(" k ")").test(format)) {format = format.replace(RegExp.$1,RegExp.$1.length == 1 ? o[k] :("00" o[k]).substr(("" o[k]).length));}}return format;}
由于我们常常需要对时间进行比较,因此jQuery也提供了比较时间的函数:
// 比较时间大小var date1 = new Date('2021-01-01');var date2 = new Date('2021-06-01');if (date1.getTime() >date2.getTime()) {console.log('date1 晚于 date2');} else if (date1.getTime()< date2.getTime()){console.log('date1 早于 date2');} else {console.log('date1 和 date2 相同');}
总之,Jquery提供的时间函数非常方便实用,可以满足我们各种时间处理的需求。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。


