-
Notifications
You must be signed in to change notification settings - Fork 7
/
lately.js
57 lines (57 loc) · 2.03 KB
/
lately.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Lately.js - Native JavaScript, only 800Byte but simple and easy to use Timeago plugin
*
* @name Lately.js
* @version 2.5.2
* @author Tokin (Tokinx)
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*
* For usage and examples, visit:
* https://tokinx.github.io/lately/
*
* Copyright (c) 2017, biji.io
*/
(() => {
window.Lately = new function () {
this.lang = {
second: "秒",
minute: "分钟",
hour: "小时",
day: "天",
month: "个月",
year: "年",
ago: "前",
error: "NaN"
};
const format = (date) => {
date = new Date(_val(date));
const floor = (num, _lang) => Math.floor(num) + _lang,
obj = new function () {
this.second = (Date.now() - date.getTime()) / 1000;
this.minute = this.second / 60;
this.hour = this.minute / 60;
this.day = this.hour / 24;
this.month = this.day / 30;
this.year = this.month / 12
},
key = Object.keys(obj).reverse().find(_ => obj[_] >= 1);
return (key ? floor(obj[key], this.lang[key]) : this.lang.error) + this.lang.ago;
},
_val = (date) => {
date = new Date(date && (typeof date === 'number' ? date : date.replace(/-/g, '/').replace('T', ' ')));
return isNaN(date.getTime()) ? false : date.getTime();
};
return {
init: ({ target = "time", lang } = {}) => {
if (lang) this.lang = lang;
for (let el of document.querySelectorAll(target)) {
const date = _val(el.dateTime) || _val(el.title) || _val(el.innerHTML) || 0;
if (!date) return;
el.title = new Date(date).toLocaleString();
el.innerHTML = format(date);
}
},
format
}
}
})();