-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdates.js
123 lines (108 loc) · 3.07 KB
/
dates.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
var MONTHS = [
[1, "January"],
[2, "February"],
[3, "March"],
[4, "April"],
[5, "May"],
[6, "June"],
[7, "July"],
[8, "August"],
[9, "September"],
[10, "October"],
[11, "November"],
[12, "December"]
];
function friendly(arr) {
function convertDate(date) {
var convertedDate = date.split('-');
for ( var i = 0; i < convertedDate.length; i++) {
convertedDate[i] = parseInt(convertedDate[i]);
}
return convertedDate;
}
function prepDates(dates) {
var startDate = dates[0],
endDate = dates[1],
workingStartDate = startDate,
workingEndDate = endDate,
currentYear = new Date().getFullYear();
if ( JSON.stringify(startDate) === JSON.stringify(endDate) ) {
workingEndDate = [];
}
// start and end in same month, same year
else if (startDate[0] === endDate[0] && startDate[1] === endDate[1]) {
workingEndDate = [ endDate[2] ];
}
// start & end within 1 year
else if (startDate[0] === endDate[0] ||
(startDate[0] === endDate[0]-1 && startDate[1] > endDate[1]) ||
(startDate[0] === endDate[0]-1 && startDate[1] === endDate[1]) && startDate[2] > endDate[2] ) {
// if start year is current year
workingEndDate = [ endDate[1], endDate[2] ];
}
if ( startDate[0] === currentYear && workingEndDate.length < 3 ) {
workingStartDate.shift();
}
return [workingStartDate, workingEndDate];
}
function monthNumToName(month) {
for( var i = 0; i < MONTHS.length; i++ ) {
if (month == MONTHS[i][0]) {
month = MONTHS[i][1];
}
}
return month;
}
function convertToOrd(num) {
var numStr = num.toString();
switch ( numStr.charAt(numStr.length-1) ) {
case "1":
numStr = numStr + "st";
break;
case "2":
numStr = numStr + "nd";
break;
case "3":
if (numStr === "13") {
numStr = numStr + "th";
} else {
numStr = numStr + "rd";
}
break;
default:
numStr = numStr + "th";
break;
}
return numStr;
}
function formatDates(dates) {
for (var i = 0; i < dates.length; i++ ) {
switch (dates[i].length) {
case 3:
dates[i][0] = dates[i][0].toString(); // year
dates[i][1] = monthNumToName(dates[i][1]); // month
dates[i][2] = convertToOrd(dates[i][2]); // date
dates[i] = dates[i][1] + " " + dates[i][2] + ", " + dates[i][0];
break;
case 2:
dates[i][0] = monthNumToName(dates[i][0]); // month
dates[i][1] = convertToOrd(dates[i][1]); // date
dates[i] = dates[i][0] + " " + dates[i][1];
break;
case 1:
dates[i][0] = convertToOrd(dates[i][0]); // date
dates[i] = dates[i][0];
break;
}
}
if (dates[1].length === 0) {
return [ dates[0] ];
}
return dates;
}
var dates = [ convertDate(arr[0]), convertDate(arr[1]) ];
dates = prepDates(dates);
dates = formatDates(dates);
return dates;
}
friendly(['2016-07-01', '2016-07-04']);