-
Notifications
You must be signed in to change notification settings - Fork 16
/
htime.ms
59 lines (54 loc) · 1.39 KB
/
htime.ms
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
58
59
export class HumanTime {
function now() {
return this._now || (this._now = (new Date()).getTime());
}
function secondsAgo(t) {
return Math.floor((self.now() - t)/1000);
}
function timeDistance(time) {
var now = this.now();
var sec = 0, min = 0, hr = 0, day = 0;
sec = this.secondsAgo(time);
if (isNaN(sec)) => '---';
if (sec >= 60) {
min = Math.floor(sec/60);
sec = sec % 60;
if (min >= 60) {
hr = Math.floor(min/60);
min = min % 60;
if (hr >= 24) {
day = Math.floor(hr/24);
hr = hr % 24;
}
}
}
var hrMinSec = [
['d', day],
['h', hr],
['m', min],
['s', sec]
];
var timeTxt = '';
var nStr;
foreach (var division in hrMinSec) {
var t = division[1];
if (t === 0 && division[0] != 's') continue;
nStr = t.toString();
timeTxt += (nStr + division[0] + ' ');
}
return timeTxt;
};
function dateTime(t) {
if (typeof t != 'number') return '';
var d = new Date(t);
var date = [d.getMonth(), d.getDay(), d.getFullYear()].join('-');
var pad = #(t) {
if (t < 10) => '0' + t.toString();
else return t.toString();
}
var minute = pad(d.getMinutes());
var second = pad(d.getSeconds());
var time = [d.getHours(), minute, second].join(':');
return date + ' ' + time;
};
}