forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformatDate.as
More file actions
409 lines (368 loc) · 12.2 KB
/
formatDate.as
File metadata and controls
409 lines (368 loc) · 12.2 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package utils.date {
import utils.conversion.minutesToSeconds;
import utils.number.addLeadingZeroes;
import utils.number.format;
import utils.number.getOrdinalSuffix;
/**
* Formats a Date object for display. Acts almost identically to the PHP date() function.
* You can prevent a recognized character in the format string from being expanded by escaping it with a preceding ^.
* <table border="1">
* <tr>
* <th style="width:150px;">Format character</th>
* <th>Description</th>
* <th style="width:200px;">Example returned values</th>
* </tr>
* <tr>
* <td>d</td>
* <td>Day of the month, 2 digits with leading zeros.</td>
* <td>01 to 31</td>
* </tr>
* <tr>
* <td>D</td>
* <td>A textual representation of a day, three letters.</td>
* <td>Mon through Sun</td>
* </tr>
* <tr>
* <td>j</td>
* <td>Day of the month without leading zeros.</td>
* <td>1 to 31</td>
* </tr>
* <tr>
* <td>l</td>
* <td>A full textual representation of the day of the week.</td>
* <td>Sunday through Saturday</td>
* </tr>
* <tr>
* <td>N</td>
* <td>ISO-8601 numeric representation of the day of the week.</td>
* <td>1 (for Monday) through 7 (for Sunday)</td>
* </tr>
* <tr>
* <td>S</td>
* <td>English ordinal suffix for the day of the month, 2 characters.</td>
* <td>st, nd, rd or th</td>
* </tr>
* <tr>
* <td>w</td>
* <td>Numeric representation of the day of the week.</td>
* <td>0 (for Sunday) through 6 (for Saturday)</td>
* </tr>
* <tr>
* <td>z</td>
* <td>The day of the year (starting from 0).</td>
* <td>0 through 365</td>
* </tr>
* <tr>
* <td>W</td>
* <td>ISO-8601 week number of year, weeks starting on Monday.</td>
* <td>Example: 42 (the 42nd week in the year)</td>
* </tr>
* <tr>
* <td>F</td>
* <td>A full textual representation of a month, such as January or March.</td>
* <td>January through December</td>
* </tr>
* <tr>
* <td>m</td>
* <td>Numeric representation of a month, with leading zeros.</td>
* <td>01 through 12</td>
* </tr>
* <tr>
* <td>M</td>
* <td>A short textual representation of a month, three letters.</td>
* <td>Jan through Dec</td>
* </tr>
* <tr>
* <td>n</td>
* <td>Numeric representation of a month, without leading zeros.</td>
* <td>1 through 12</td>
* </tr>
* <tr>
* <td>t</td>
* <td>Number of days in the given month.</td>
* <td>28 through 31</td>
* </tr>
* <tr>
* <td>L</td>
* <td>Determines if it is a leap year.</td>
* <td>1 if it is a leap year, 0 otherwise</td>
* </tr>
* <tr>
* <td>o or Y</td>
* <td>A full numeric representation of a year, 4 digits.</td>
* <td>Examples: 1999 or 2003</td>
* </tr>
* <tr>
* <td>y</td>
* <td>A two digit representation of a year.</td>
* <td>Examples: 99 or 03</td>
* </tr>
* <tr>
* <td>a</td>
* <td>Lowercase Ante meridiem and Post meridiem.</td>
* <td>am or pm</td>
* </tr>
* <tr>
* <td>A</td>
* <td>Uppercase Ante meridiem and Post meridiem.</td>
* <td>AM or PM</td>
* </tr>
* <tr>
* <td>B</td>
* <td>Swatch Internet time.</td>
* <td>000 through 999</td>
* </tr>
* <tr>
* <td>g</td>
* <td>12-hour format of an hour without leading zeros.</td>
* <td>1 through 12</td>
* </tr>
* <tr>
* <td>G</td>
* <td>24-hour format of an hour without leading zeros.</td>
* <td>0 through 23</td>
* </tr>
* <tr>
* <td>h</td>
* <td>12-hour format of an hour with leading zeros.</td>
* <td>01 through 12</td>
* </tr>
* <tr>
* <td>H</td>
* <td>24-hour format of an hour with leading zeros.</td>
* <td>00 through 23</td>
* </tr>
* <tr>
* <td>i</td>
* <td>Minutes with leading zeros.</td>
* <td>00 to 59</td>
* </tr>
* <tr>
* <td>s</td>
* <td>Seconds, with leading zeros.</td>
* <td>00 through 59</td>
* </tr>
* <tr>
* <td>I</td>
* <td>Determines if the date is in daylight saving time.</td>
* <td>1 if Daylight Saving Time, 0 otherwise</td>
* </tr>
* <tr>
* <td>O</td>
* <td>Difference to coordinated universal time (UTC) in hours.</td>
* <td>Example: +0200</td>
* </tr>
* <tr>
* <td>P</td>
* <td>Difference to Greenwich time (GMT/UTC) in hours with colon between hours and minutes.</td>
* <td>Example: +02:00</td>
* </tr>
* <tr>
* <td>e or T</td>
* <td>Timezone abbreviation.</td>
* <td>Examples: EST, MDT</td>
* </tr>
* <tr>
* <td>Z</td>
* <td>Timezone offset in seconds.</td>
* <td>-43200 through 50400</td>
* </tr>
* <tr>
* <td>c</td>
* <td>ISO 8601 date.</td>
* <td>2004-02-12T15:19:21+00:00</td>
* </tr>
* <tr>
* <td>r</td>
* <td>RFC 2822 formatted date.</td>
* <td>Example: Thu, 21 Dec 2000 16:01:07 +0200</td>
* </tr>
* <tr>
* <td>U</td>
* <td>Seconds since the Unix Epoch.</td>
* <td>Example: 1171479314</td>
* </tr>
* </table>
* Example code:
* <pre>
* trace(DateUtils.formatDate(new Date(), "l ^t^h^e dS ^of F Y h:i:s A"));
* </pre>
* @param dateToFormat Date object you wish to format
* @param formatString Format of the outputted date String. See the format characters options above.
* @author Aaron Clinger
* @author Shane McCartney
* @author David Nelson
*/
public function formatDate(dateToFormat:Date, formatString:String):String {
var out:String = "";
var c:String;
var i:int = -1;
var l:uint = formatString.length;
var t:Number;
while(++i < l) {
c = formatString.substr(i, 1);
if(c == "^") {
out += formatString.substr(++i, 1);
}
else {
switch(c) {
case "d" :
// Day of the month, 2 digits with leading zeros
out += addLeadingZeroes(dateToFormat.getDate());
break;
case "D" :
// A textual representation of a day, three letters
out += getDayAbbrName(dateToFormat.getDay());
break;
case "j" :
// Day of the month without leading zeros
out += String(dateToFormat.getDate());
break;
case "l" :
// A full textual representation of the day of the week
out += getDayAsString(dateToFormat.getDay());
break;
case "N" :
// ISO-8601 numeric representation of the day of the week
t = dateToFormat.getDay();
if(t == 0) t = 7;
out += String(t);
break;
case "S" :
// English ordinal suffix for the day of the month, 2 characters
out += getOrdinalSuffix(dateToFormat.getDate());
break;
case "w" :
// Numeric representation of the day of the week
out += String(dateToFormat.getDay());
break;
case "z" :
// The day of the year (starting from 0)
out += String(addLeadingZeroes(getDayOfTheYear(dateToFormat)));
break;
case "W" :
// ISO-8601 week number of year, weeks starting on Monday
out += String(addLeadingZeroes(getWeekOfTheYear(dateToFormat)));
break;
case "F" :
// A full textual representation of a month, such as January or March
out += getMonthName(dateToFormat.getMonth());
break;
case "m" :
// Numeric representation of a month, with leading zeros
out += addLeadingZeroes(dateToFormat.getMonth() + 1);
break;
case "M" :
// A short textual representation of a month, three letters
out += getMonthAbbrName(dateToFormat.getMonth());
break;
case "n" :
// Numeric representation of a month, without leading zeros
out += String((dateToFormat.getMonth() + 1));
break;
case "t" :
// Number of days in the given month
out += String(getDaysInMonth(dateToFormat.getMonth(), dateToFormat.getFullYear()));
break;
case "L" :
// Whether it is a leap year
out += (isLeapYear(dateToFormat.getFullYear())) ? "1" : "0";
break;
case "o" :
case "Y" :
// A full numeric representation of a year, 4 digits
out += String(dateToFormat.getFullYear());
break;
case "y" :
// A two digit representation of a year
out += String(dateToFormat.getFullYear()).substr(-2);
break;
case "a" :
// Lowercase Ante meridiem and Post meridiem
out += getMeridiem(dateToFormat.getHours()).toLowerCase();
break;
case "A" :
// Uppercase Ante meridiem and Post meridiem
out += getMeridiem(dateToFormat.getHours());
break;
case "B" :
// Swatch Internet time
out += format(getInternetTime(dateToFormat), 3, null, "0");
break;
case "g" :
// 12-hour format of an hour without leading zeros
t = dateToFormat.getHours();
if(t == 0) {
t = 12;
} else if(t > 12) {
t -= 12;
}
out += String(t);
break;
case "G" :
// 24-hour format of an hour without leading zeros
out += String(dateToFormat.getHours());
break;
case "h" :
// 12-hour format of an hour with leading zeros
t = dateToFormat.getHours() + 1;
if(t == 0) {
t = 12;
} else if(t > 12) {
t -= 12;
}
out += addLeadingZeroes(t);
break;
case "H" :
// 24-hour format of an hour with leading zeros
out += addLeadingZeroes(dateToFormat.getHours());
break;
case "i" :
// Minutes with leading zeros
out += addLeadingZeroes(dateToFormat.getMinutes());
break;
case "s" :
// Seconds, with leading zeros
out += addLeadingZeroes(dateToFormat.getSeconds());
break;
case "I" :
// Whether or not the date is in daylights savings time
out += (isDaylightSavings(dateToFormat)) ? "1" : "0";
break;
case "O" :
// Difference to Greenwich time (GMT/UTC) in hours
out += getFormattedDifferenceFromUTC(dateToFormat);
break;
case "P" :
out += getFormattedDifferenceFromUTC(dateToFormat, ":");
break;
case "e" :
case "T" :
// Timezone identifier
out += getTimezone(dateToFormat);
break;
case "Z" :
// Timezone offset (GMT/UTC) in seconds.
out += String(int(minutesToSeconds(dateToFormat.getTimezoneOffset())));
break;
case "c" :
// ISO 8601 date
out += dateToFormat.getFullYear() + "-" + addLeadingZeroes(dateToFormat.getMonth() + 1) + "-" + addLeadingZeroes(dateToFormat.getDate()) + "T" + addLeadingZeroes(dateToFormat.getHours()) + ":" + addLeadingZeroes(dateToFormat.getMinutes()) + ":" + addLeadingZeroes(dateToFormat.getSeconds()) + getFormattedDifferenceFromUTC(dateToFormat, ":");
break;
case "r" :
// RFC 2822 formatted date
out += getDayAbbrName(dateToFormat.getDay()) + ", " + dateToFormat.getDate() + " " + getMonthAbbrName(dateToFormat.getMonth()) + " " + dateToFormat.getFullYear() + " " + addLeadingZeroes(dateToFormat.getHours()) + ":" + addLeadingZeroes(dateToFormat.getMinutes()) + ":" + addLeadingZeroes(dateToFormat.getSeconds()) + " " + getFormattedDifferenceFromUTC(dateToFormat);
break;
case "U" :
// Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
t = int(dateToFormat.getTime() / 1000);
out += String(t);
break;
default :
out += formatString.substr(i, 1);
}
}
}
return out;
}
}