-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnichi.js
112 lines (108 loc) · 3.76 KB
/
nichi.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
MIT License
Copyright (c) 2012-2013 Jongmin T. Moon <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var nichi = (function(input) {
var date,
ms = {
sec : 1000,
min : 60000,
hr : 3600000,
day : 86400000,
wk : 604800000,
},
daysInMonth = [
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
];
function set(val) {
/* Set date obj */
/* TODO: Validate */
if(val instanceof Date) date = val;
else {
date = new Date(val);
}
}
function update(base, mult) {
var current = date.getTime();
current += base * mult;
date = new Date(current);
}
if(!input) {
/* Set new current date obj if no input */
date = new Date();
}
else set(input);
return {
set : set,
get : function() {
return date;
},
sec : function(sec) {
update(ms.sec, sec);
return this;
},
min : function(min) {
update(ms.min, min);
return this;
},
hr : function(hr) {
update(ms.hr, hr);
return this;
},
day : function(day) {
update(ms.day, day);
return this;
},
wk : function(wk) {
update(ms.wk, wk);
return this;
},
elapsed : function() {
var was = date.getTime(),
now = Date.now(),
passed = now - was, //Time passed in ms
unit, //Unit of measurement (e.g. 'day')
recent = false, //Time passed is less than a second.
mod, //Remainder
floor; //Math.floor() of time passed
function calcElapsed(measure, time, num) {
/* Calculates elapsed time based on unit */
unit = measure;
floor = Math.floor(time / num);
mod = time % num;
}
if(passed >= ms.wk) {
/* Check if weeks passed */
calcElapsed('week', passed, ms.wk);
}
else if(passed >= ms.day) {
/* Check if days passed */
calcElapsed('day', passed, ms.day);
}
else if(passed >= ms.hr) {
/* Check if hours passed */
calcElapsed('hour', passed, ms.hr);
}
else if(passed >= ms.min) {
/* Check if minutes passed */
calcElapsed('minute', passed, ms.min);
}
else {
/* Check if seconds passed */
if(passed >= ms.sec) {
calcElapsed('second', passed, ms.sec);
}
else recent = true;
}
if(recent) return 'just now';
else {
if(floor > 1) unit += 's';
if(floor === 1) return 'a ' + unit + ' ago'
else return floor + ' ' + unit + ' ago';
}
}
}
});