-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeago.js
83 lines (69 loc) · 2.61 KB
/
timeago.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
module.exports = {
short_month: ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"],
long_month: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
long_days: ["", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
suffix_st: [1, 21, 31],
suffix_nd: [2, 22],
suffix_rd: [3, 23],
getDifference: function(date){
date = new Date(date);
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 3600);
if (interval > 23) {
return date.getDate()+" "+this.short_month[date.getMonth()]+" "+date.getFullYear();
}
if (interval >= 1) {
return interval + " hrs ago";
}
interval = seconds / 60;
if (interval > 1) {
if(Math.floor(interval) == 1) return "a minute ago";
return Math.floor(interval) + " minutes ago";
}
return "a few seconds ago";
},
zeroPad: function(size, digit){
if((digit.toString()).length < size) return "0"+digit;
return digit;
},
getAMPM: function(date){
return date.getHours() > 11 ? "pm" : "am";
},
getDateWithSuffix: function(date){
var dateNo = date.getDate();
if(this.suffix_st.indexOf(dateNo) > -1) return dateNo+"st";
if(this.suffix_nd.indexOf(dateNo) > -1) return dateNo+"nd";
if(this.suffix_rd.indexOf(dateNo) > -1) return dateNo+"rd";
return dateNo+"th";
},
get12Hour: function(date){
if(date.getHours() == 0) return 12;
if(date.getHours() > 12) return date.getHours() - 12;
return date.getHours();
},
getTime: function(date){
return this.get12Hour(date)+
":"+
this.zeroPad(2, date.getMinutes())+
":"+
this.zeroPad(2, date.getSeconds())+
this.getAMPM(date);
},
getDate: function(date){
return this.long_days[date.getDay()]+
", "+
this.getDateWithSuffix(date)+
" "+
this.long_month[date.getMonth()]+
" "+
date.getFullYear();
},
getStamp: function(date){
date = new Date(date);
return this.getDate(date)+", "+this.getTime(date);
//return date.getHours()+":"+date.getMinutes()+" "+date.getDate()+" "+this.short_month[date.getMonth()]+" "+date.getFullYear();
},
html: function(date){
return "<time title='"+this.getStamp(date)+"'>"+this.getDifference(date)+"</time>";
}
}