Repository navigation
Expand file tree
/
Copy pathangular-sundial.js
More file actions
209 lines (176 loc) · 7.95 KB
/
Copy pathangular-sundial.js
File metadata and controls
209 lines (176 loc) · 7.95 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
(function () {
'use strict';
angular
.module('ngSundial', [])
.provider('sundial', sundial);
sundial.$inject = [];
function sundial() {
var locale = 'en-US',
timeStandard = '24';
return {
config: config,
$get: [factory]
};
/**
* Sets the locale and timeStandard variables
* @param {Object} options (Contains the properties to be set)
* @param {Object} options.locale (The locale to be used in all dates)
* @param {Object} options.timeStandard (The standard notation to be used)
*/
function config(options) {
options = options || {};
locale = options.locale || locale;
timeStandard = options.timeStandard || timeStandard;
}
function factory() {
var service = {
getDate : getDateOnly,
getDateWithWeek: getDateWithWeek,
getDateWithTime: getDateWithTime,
getWeekDay : getWeekDay,
getMilitaryTime : getMilitaryTime,
getAmPmTime: getAmPmTime,
getTime : getTime,
getGMT: getGMT
};
return service;
/* Public functions */
/**
* Builds a date today format from timestamp string specified
* @param {String} timestamp (The date string or Date instance)
* @param {Object} options (Contains the properties of the date to build)
* @param {Object} options.month (Month type: 'numeric, 2-digit, narrow, short or long')
* @param {Object} options.day (Day type: 'numeric or 2-digit')
* @param {Object} options.year (Year type: 'numeric or 2-digit')
* @return {String} The built date
*/
function getDateOnly(timestamp, options) {
var date = getDate(timestamp);
options = buildDateOptions(options);
return date.toLocaleDateString(locale, options);
}
/**
* Builds a date today format from timestamp string specified
* @param {String} timestamp (The date string or Date instance)
* @param {String} weekday (Weekday type: 'narrow, short or long')
* @param {Object} options (Contains the properties of the date to build)
* @param {Object} options.month (Month type: 'numeric, 2-digit, narrow, short or long')
* @param {Object} options.day (Day type: 'numeric or 2-digit')
* @param {Object} options.year (Year type: 'numeric or 2-digit')
* @return {String} The built date
*/
function getDateWithWeek(timestamp, weekday, options) {
var date = getDate(timestamp);
options = buildDateOptions(options);
options.weekday = (weekday) ? weekday : 'long';
return date.toLocaleDateString(locale, options);
}
/**
* Builds a date today format from timestamp string specified
* @param {String} timestamp (The date string or Date instance)
* @param {Object} time (Contains the properties for the time)
* @param {Object} time.hour (Hour type: 'numeric or 2-digit')
* @param {Object} time.minute (Minute type: 'numeric or 2-digit')
* @param {Object} options (Contains the properties of the date to build)
* @param {Object} options.month (Month type: 'numeric, 2-digit, narrow, short or long')
* @param {Object} options.weekday (Weekday type: 'narrow, short or long')
* @return {String} The built date
*/
function getDateWithTime(timestamp, time, options) {
var date = getDate(timestamp);
options = buildDateOptions(options);
// Ensures that the time is always an object
time = time || {};
options.hour = (time.hour) ? time.hour : '2-digit';
options.minute = (time.minute) ? time.minute : '2-digit';
return date.toLocaleDateString(locale, options);
}
/**
* Gets the weekday with the format based on weekDay param
* @param {String} timestamp (The date string or Date instance)
* @param {String} weekDay (Weekday type 'narrow, long or short')
* @return {String}
*/
function getWeekDay(timestamp, weekDay) {
var date = getDate(timestamp);
weekDay = (weekDay === undefined) ? 'short' : weekDay;
return date.toLocaleDateString(locale, {weekday: weekDay})
};
/**
* Creates a military time format for the timestamp string specified
* @param {String} timestamp (The date string or Date instance)
* @return {String}
*/
function getMilitaryTime(timestamp) {
var time = getTimeObject(timestamp),
hrs = prefixZero(time.hrs);
return (hrs + ':' + time.mins);
}
/**
* Creates an am/pm time format for the timestamp string specified
* @param {String} timestamp (The date string or Date instance)
* @return {String}
*/
function getAmPmTime(timestamp) {
var time = getTimeObject(timestamp),
hrs = time.hrs,
period = hrs >= 12 ? 'pm' : 'am';
hrs = (hrs = hrs % 12) ? hrs : 12,
hrs = prefixZero(hrs);
return (hrs + ':' + time.mins + period);
}
/**
* Gets the time format according to the timeStandard set
* @param {String} timestamp (The date string or Date instance)
* @return {String}
*/
function getTime(timestamp) {
var time = undefined;
if (timeStandard === '24') {
time = getMilitaryTime(timestamp);
} else if (timeStandard === '12') {
time = getAmPmTime(timestamp);
} else {
time = 'Invalid Time Format.';
}
return time;
}
/**
* Gets the GMT from the timestamp string specified
* @param {String} timestamp (The date string or Date instance)
* @return {String}
*/
function getGMT(timestamp) {
return getDate(timestamp).toString().match(/([A-Z]+[\+-][0-9]+)/)[1];
}
/* Private functions */
// If the passed timestamp is already a Date object, then there is
// no need to instantiate a new Date object again, just return it
function getDate(timestamp) {
return (timestamp instanceof Date) ? timestamp : new Date(timestamp);
}
// Assign default values to options that are not existing
function buildDateOptions(options) {
// Ensures that the options is always an object
options = options || {};
return {
month: (options.month) ? options.month : 'short',
year: (options.year) ? options.year: 'numeric',
day: (options.day) ? options.day: 'numeric'
};
}
// Get the hour and minute from the date
function getTimeObject(timestamp) {
var date = getDate(timestamp);
return {
hrs: date.getHours(),
mins: prefixZero(date.getMinutes())
};
}
// Adds a '0' character to the passed string
function prefixZero(string) {
return ('0' + string).slice(-2);
}
}
}
})();