-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
147 lines (130 loc) · 5.49 KB
/
index.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var timeago = function(){};
timeago.prototype.simple = function(date_time) {
// today date and time in milliseconds
var today = Date.now();
var dateParse = Date.parse(date_time);
//We will perform some test - if there is error, we will throw error to console and exit, no change will be on the data.
try {
// We need to check if we able to parse the Date (if the result is NaN, this is an issue)
if(dateParse !== dateParse) throw "timeago-simple: Please check date and time format! Unable to parse the date & time: " + date_time;
}
catch(err) {
console.error(err);
return (date_time);
}
if((dateParse - today) < 0) {
return pastCalc(date_time);
} else {
return futureCalc(date_time);
}
};
// General help functions for time calculations
function pastCalc(timeData){
// today date and time in milliseconds
var today = Date.now();
// parsing post date and time into milliseconds format
timeData = Date.parse(timeData);
var seconds = (today - timeData) / 1000;
var minutes = (seconds / 60);
var hours = (seconds / 3600);
if(seconds < 60 && minutes < 1) {
return (seconds === 1 ? Math.round(seconds) + " second ago" : Math.round(seconds) + " seconds ago");
}
if(minutes < 60 && hours < 1) {
return (minutes === 1 ? Math.round(minutes) + " minute ago" : Math.round(minutes) + " minutes ago");
}
if(hours > 24){
var days = hours / 24;
if (days > 30) {
var month = days / 30;
if (month > 12) {
var years = month / 12;
if (years > 0) {
return (years === 1 ? Math.ceil(years) + " year ago" : Math.ceil(years) + " years ago");
}
}
return (Math.round(month) + " month ago");
}
return (days === 1 ? Math.round(days) + " day ago" : Math.round(days) + " days ago");
} else {
return (hours === 1 ? Math.round(hours) + " hour ago" : Math.round(hours) + " hours ago");
}
}
function futureCalc(timeData){
// today date and time in milliseconds
var today = Date.now();
// parsing post date and time into milliseconds format
timeData = Date.parse(timeData);
var seconds = (timeData - today) / 1000;
var minutes = (seconds / 60);
var hours = (seconds / 3600);
if(seconds < 60 && minutes < 1) {
return (seconds === 1 ? "in " + Math.round(seconds) + " second" : "in " + Math.round(seconds) + " seconds");
}
if(minutes < 60 && hours < 1) {
return (minutes === 1 ? "in " + Math.round(minutes) + " minute" : "in " + Math.round(minutes) + " minutes");
}
if(hours > 24){
var days = hours / 24;
if (days > 30) {
var month = days / 30;
if (month > 12) {
var years = month / 12;
if (years > 0) {
return (years === 1 ? "in " + Math.ceil(years) + " year" : "in " + Math.ceil(years) + " years");
}
}
return ("in " + Math.round(month) + " month");
}
return (days === 1 ? "in " + Math.round(days) + " day" : "in " + Math.round(days) + " days");
} else {
return (hours === 1 ? "in " + Math.round(hours) + " hour" : "in " + Math.round(hours) + " hours");
}
}
// Future calculation
timeago.prototype.future = function(timeData) {
console.warn("timeago-simple: .future function is depricated! Please use .simple for both past and future dates.");
// today date and time in milliseconds
var today = Date.now();
//We will perform some test - if there is error, we will throw error to console and exit, no change will be on the data.
try {
// We need to check if we able to parse the Date (if the result is NaN, this is an issue)
if(Date.parse(timeData) !== Date.parse(timeData)) throw "timeago-simple: Please check date and time format! Unable to parse the date & time: " + timeData;
// Need to check if it's really future date to parse
if((Date.parse(timeData) - today) < 0) throw "timeago-simple: Looks like it's more relevant case for timeago.simple";
}
catch(err) {
console.error(err);
return (timeData);
}
// parsing post date and time into milliseconds format
timeData = Date.parse(timeData);
var seconds = (timeData - today) / 1000;
var minutes = (seconds / 60);
var hours = (seconds / 3600);
/* istanbul ignore if */
if(seconds < 60 && minutes < 1) {
return (seconds === 1 ? "in " + Math.round(seconds) + " second" : "in " + Math.round(seconds) + " seconds");
}
/* istanbul ignore if */
if(minutes < 60 && hours < 1) {
return (minutes === 1 ? "in " + Math.round(minutes) + " minute" : "in " + Math.round(minutes) + " minutes");
}
/* istanbul ignore if */
if(hours > 24){
var days = hours / 24;
if (days > 30) {
var month = days / 30;
if (month > 12) {
var years = month / 12;
if (years > 0) {
return (years === 1 ? "in " + Math.ceil(years) + " year" : "in " + Math.ceil(years) + " years");
}
}
return ("in " + Math.round(month) + " month");
}
return (days === 1 ? "in " + Math.round(days) + " day" : "in " + Math.round(days) + " days");
}
return (hours === 1 ? "in " + Math.round(hours) + " hour" : "in " + Math.round(hours) + " hours");
};
module.exports = new timeago();