前端 时间个性化 插件 jquery.timeago.js
关键词 :
时间格式化 刚刚 N分钟前 N小时前 N天前 N月前 N年前
MM-dd hh:mm 或者 yyyy-MM-dd
前端:
< span class="time" title="2016-07-23 12:02:32"></ span > |
用法:
$(".time").timeago();
插件: jquery.timeago.js
!function (t) { function e(e) { var n = t.extend(o.settings, e) , s = t.proxy(r, this); s(), n.refreshMillis > 0 && setInterval(s, n.refreshMillis) } function r() { var e = n(this) , r = e.datetime; return isNaN(r) || t(this).text(o.inWords(r)), this } function n(e) { return e = t(e), e.data("timeago") || e.data("timeago", { datetime: o.datetime(e) }), e.data("timeago") } function s(t) { return (new Date).getTime() - t.getTime() } function i(t) { var e = new Date; return e.getMonth() > t.getMonth() || e.getDate() > t.getDate() } function a(t) { return (new Date).getFullYear() > t.getFullYear() } Date.prototype.format = function (t) { var e = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), S: this.getMilliseconds() }; /(y+)/.test(t) && (t = t.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length))); for (var r in e) new RegExp("(" + r + ")").test(t) && (t = t.replace(RegExp.$1, 1 == RegExp.$1.length ? e[r] : ("00" + e[r]).substr(("" + e[r]).length))); return t } ; var o = { settings: { refreshMillis: 6e4, relative: !0, strings: { suffixAgo: "前", seconds: "刚刚", minute: "1分钟", minutes: "%d分钟", hour: "1小时", hours: "%d小时", days: "%d天", months: "%d月", years: "%d年", numbers: [] }, yearsAgoFormat: "yyyy-MM-dd", daysAgoFormat: "MM-dd hh:mm" }, inWords: function (e) { function r(e, r) { var n = t.isFunction(e) ? e(r, u) : e , s = g.numbers && g.numbers[r] || r; return n.replace(/%d/i, s) } var n = o.settings.relative; if (!n && a(e)) return e.format(this.settings.yearsAgoFormat); if (!n && i(e)) return e.format(this.settings.daysAgoFormat); var u = s(e) , g = this.settings.strings , h = g.suffixAgo , d = Math.abs(u) / 1e3 , l = d / 60 , f = l / 60 , m = f / 24 , c = m / 30 , M = m / 365; return words = 60 > d ? r(g.seconds, Math.floor(d)) : 60 > l ? r(g.minutes, Math.floor(l)) : 24 > f ? r(g.hours, Math.floor(f)) : 30 > m ? r(g.days, Math.floor(m)) : 365 > m ? r(g.months, Math.floor(c)) : r(g.years, Math.floor(M)), "刚刚" == words ? words : words + h }, parse: function (e) { var r = t.trim(e); return r = r.replace(/\.\d+/, ""), r = r.replace(/-/, "/").replace(/-/, "/"), r = r.replace(/T/, " ").replace(/Z/, " UTC"), r = r.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"), new Date(r) }, datetime: function (e) { var r = t(e).attr(o.isTime(e) ? "datetime" : "title"); return o.parse(r) }, isTime: function (e) { return "time" === t(e).get(0).tagName.toLowerCase() } }; t.fn.timeago = function (t) { return this.each(function () { e.call(this, t) }), this } }(window.jQuery)
分类:
jQuery
, Javascript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库