-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateline.js
136 lines (110 loc) · 2.85 KB
/
dateline.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
(function () {
var DAY_NAMES = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
var MONTH_NAMES = [
"Jan.",
"Feb.",
"March",
"April",
"May",
"June",
"July",
"Aug.",
"Sept.",
"Oct.",
"Nov.",
"Dec.",
];
var ONE_DAY_IN_MS = 1000 * 60 * 60 * 24;
var Dateline = function (dateObj) {
if (dateObj == null) {
dateObj = new Date();
}
dateObj.getAPTime = function (options) {
if (options == null) {
options = {};
}
var hours = dateObj.getHours();
var minutes = dateObj.getMinutes();
// Special cases: "midnight" and "noon"
if (hours === 0 && minutes === 0) {
return "midnight";
} else if (hours === 12 && minutes === 0) {
return "noon";
}
var timeOfDay = hours < 12 ? "a.m." : "p.m.";
var hour = formatHours(hours);
// Don't show minutes at the top of the hour by default
if (showMinutes(minutes, options)) {
return hour + " " + timeOfDay;
}
var minute = formatMinutes(minutes);
return hour + ":" + minute + " " + timeOfDay;
};
dateObj.getAPDate = function (options) {
if (options == null) {
options = {};
}
var month = dateObj.getMonth();
var date = dateObj.getDate();
var year = dateObj.getFullYear();
var monthName = MONTH_NAMES[month];
if (useDayName(dateObj, options)) {
return getDayOfWeek(dateObj);
} else if (showYear(year, options)) {
return monthName + " " + date;
} else {
return monthName + " " + date + ", " + year;
}
};
return dateObj;
};
// Export for Node or browser
if (
typeof module !== "undefined" && module !== null ? module.exports : void 0
) {
module.exports = Dateline;
} else {
this.Dateline = Dateline;
}
// # Time Helpers
function formatHours(hours) {
if (hours === 0) {
return 12;
} else if (hours > 12) {
return hours - 12;
} else {
return hours;
}
}
function formatMinutes(minutes) {
return minutes < 10 ? "0" + minutes : minutes.toString();
}
function isTopOfHour(minutes) {
return minutes === 0;
}
function showMinutes(minutes, options) {
return isTopOfHour(minutes) && options.includeMinutes == null;
}
// # Date Helpers
function showYear(year, options) {
return year === new Date().getFullYear() && options.includeYear == null;
}
function getDayOfWeek(dateObj) {
return DAY_NAMES[dateObj.getDay()];
}
function useDayName(dateObj, options) {
return options.useDayNameForLastWeek != null && withinSevenDays(dateObj);
}
function withinSevenDays(dateObj) {
var diffInDays = (dateObj - new Date()) / ONE_DAY_IN_MS;
return -7 < diffInDays && diffInDays < 0;
}
}).call(this);