Project

General

Profile

Statistics
| Branch: | Revision:

colonymech / docs / www / colonyscout / internal / jeditable / js / date.js @ f59acf11

History | View | Annotate | Download (10.8 KB)

1
/*
2
 * Date prototype extensions. Doesn't depend on any
3
 * other code. Doens't overwrite existing methods.
4
 *
5
 * Adds dayNames, abbrDayNames, monthNames and abbrMonthNames static properties and isLeapYear,
6
 * isWeekend, isWeekDay, getDaysInMonth, getDayName, getMonthName, getDayOfYear, getWeekOfYear,
7
 * setDayOfYear, addYears, addMonths, addDays, addHours, addMinutes, addSeconds methods
8
 *
9
 * Copyright (c) 2006 Jörn Zaefferer and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
10
 *
11
 * Additional methods and properties added by Kelvin Luck: firstDayOfWeek, dateFormat, zeroTime, asString, fromString -
12
 * I've added my name to these methods so you know who to blame if they are broken!
13
 * 
14
 * Dual licensed under the MIT and GPL licenses:
15
 *   http://www.opensource.org/licenses/mit-license.php
16
 *   http://www.gnu.org/licenses/gpl.html
17
 *
18
 */
19

    
20
/**
21
 * An Array of day names starting with Sunday.
22
 * 
23
 * @example dayNames[0]
24
 * @result 'Sunday'
25
 *
26
 * @name dayNames
27
 * @type Array
28
 * @cat Plugins/Methods/Date
29
 */
30
Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
31

    
32
/**
33
 * An Array of abbreviated day names starting with Sun.
34
 * 
35
 * @example abbrDayNames[0]
36
 * @result 'Sun'
37
 *
38
 * @name abbrDayNames
39
 * @type Array
40
 * @cat Plugins/Methods/Date
41
 */
42
Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
43

    
44
/**
45
 * An Array of month names starting with Janurary.
46
 * 
47
 * @example monthNames[0]
48
 * @result 'January'
49
 *
50
 * @name monthNames
51
 * @type Array
52
 * @cat Plugins/Methods/Date
53
 */
54
Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
55

    
56
/**
57
 * An Array of abbreviated month names starting with Jan.
58
 * 
59
 * @example abbrMonthNames[0]
60
 * @result 'Jan'
61
 *
62
 * @name monthNames
63
 * @type Array
64
 * @cat Plugins/Methods/Date
65
 */
66
Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
67

    
68
/**
69
 * The first day of the week for this locale.
70
 *
71
 * @name firstDayOfWeek
72
 * @type Number
73
 * @cat Plugins/Methods/Date
74
 * @author Kelvin Luck
75
 */
76
Date.firstDayOfWeek = 1;
77

    
78
/**
79
 * The format that string dates should be represented as (e.g. 'dd/mm/yyyy' for UK, 'mm/dd/yyyy' for US, 'yyyy-mm-dd' for Unicode etc).
80
 *
81
 * @name format
82
 * @type String
83
 * @cat Plugins/Methods/Date
84
 * @author Kelvin Luck
85
 */
86
Date.format = 'dd/mm/yyyy';
87
//Date.format = 'mm/dd/yyyy';
88
//Date.format = 'yyyy-mm-dd';
89
//Date.format = 'dd mmm yy';
90

    
91
(function() {
92

    
93
        /**
94
         * Adds a given method under the given name 
95
         * to the Date prototype if it doesn't
96
         * currently exist.
97
         *
98
         * @private
99
         */
100
        function add(name, method) {
101
                if( !Date.prototype[name] ) {
102
                        Date.prototype[name] = method;
103
                }
104
        };
105
        
106
        /**
107
         * Checks if the year is a leap year.
108
         *
109
         * @example var dtm = new Date("01/12/2008");
110
         * dtm.isLeapYear();
111
         * @result true
112
         *
113
         * @name isLeapYear
114
         * @type Boolean
115
         * @cat Plugins/Methods/Date
116
         */
117
        add("isLeapYear", function() {
118
                var y = this.getFullYear();
119
                return (y%4==0 && y%100!=0) || y%400==0;
120
        });
121
        
122
        /**
123
         * Checks if the day is a weekend day (Sat or Sun).
124
         *
125
         * @example var dtm = new Date("01/12/2008");
126
         * dtm.isWeekend();
127
         * @result false
128
         *
129
         * @name isWeekend
130
         * @type Boolean
131
         * @cat Plugins/Methods/Date
132
         */
133
        add("isWeekend", function() {
134
                return this.getDay()==0 || this.getDay()==6;
135
        });
136
        
137
        /**
138
         * Check if the day is a day of the week (Mon-Fri)
139
         * 
140
         * @example var dtm = new Date("01/12/2008");
141
         * dtm.isWeekDay();
142
         * @result false
143
         * 
144
         * @name isWeekDay
145
         * @type Boolean
146
         * @cat Plugins/Methods/Date
147
         */
148
        add("isWeekDay", function() {
149
                return !this.isWeekend();
150
        });
151
        
152
        /**
153
         * Gets the number of days in the month.
154
         * 
155
         * @example var dtm = new Date("01/12/2008");
156
         * dtm.getDaysInMonth();
157
         * @result 31
158
         * 
159
         * @name getDaysInMonth
160
         * @type Number
161
         * @cat Plugins/Methods/Date
162
         */
163
        add("getDaysInMonth", function() {
164
                return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
165
        });
166
        
167
        /**
168
         * Gets the name of the day.
169
         * 
170
         * @example var dtm = new Date("01/12/2008");
171
         * dtm.getDayName();
172
         * @result 'Saturday'
173
         * 
174
         * @example var dtm = new Date("01/12/2008");
175
         * dtm.getDayName(true);
176
         * @result 'Sat'
177
         * 
178
         * @param abbreviated Boolean When set to true the name will be abbreviated.
179
         * @name getDayName
180
         * @type String
181
         * @cat Plugins/Methods/Date
182
         */
183
        add("getDayName", function(abbreviated) {
184
                return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
185
        });
186

    
187
        /**
188
         * Gets the name of the month.
189
         * 
190
         * @example var dtm = new Date("01/12/2008");
191
         * dtm.getMonthName();
192
         * @result 'Janurary'
193
         *
194
         * @example var dtm = new Date("01/12/2008");
195
         * dtm.getMonthName(true);
196
         * @result 'Jan'
197
         * 
198
         * @param abbreviated Boolean When set to true the name will be abbreviated.
199
         * @name getDayName
200
         * @type String
201
         * @cat Plugins/Methods/Date
202
         */
203
        add("getMonthName", function(abbreviated) {
204
                return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
205
        });
206

    
207
        /**
208
         * Get the number of the day of the year.
209
         * 
210
         * @example var dtm = new Date("01/12/2008");
211
         * dtm.getDayOfYear();
212
         * @result 11
213
         * 
214
         * @name getDayOfYear
215
         * @type Number
216
         * @cat Plugins/Methods/Date
217
         */
218
        add("getDayOfYear", function() {
219
                var tmpdtm = new Date("1/1/" + this.getFullYear());
220
                return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
221
        });
222
        
223
        /**
224
         * Get the number of the week of the year.
225
         * 
226
         * @example var dtm = new Date("01/12/2008");
227
         * dtm.getWeekOfYear();
228
         * @result 2
229
         * 
230
         * @name getWeekOfYear
231
         * @type Number
232
         * @cat Plugins/Methods/Date
233
         */
234
        add("getWeekOfYear", function() {
235
                return Math.ceil(this.getDayOfYear() / 7);
236
        });
237

    
238
        /**
239
         * Set the day of the year.
240
         * 
241
         * @example var dtm = new Date("01/12/2008");
242
         * dtm.setDayOfYear(1);
243
         * dtm.toString();
244
         * @result 'Tue Jan 01 2008 00:00:00'
245
         * 
246
         * @name setDayOfYear
247
         * @type Date
248
         * @cat Plugins/Methods/Date
249
         */
250
        add("setDayOfYear", function(day) {
251
                this.setMonth(0);
252
                this.setDate(day);
253
                return this;
254
        });
255
        
256
        /**
257
         * Add a number of years to the date object.
258
         * 
259
         * @example var dtm = new Date("01/12/2008");
260
         * dtm.addYears(1);
261
         * dtm.toString();
262
         * @result 'Mon Jan 12 2009 00:00:00'
263
         * 
264
         * @name addYears
265
         * @type Date
266
         * @cat Plugins/Methods/Date
267
         */
268
        add("addYears", function(num) {
269
                this.setFullYear(this.getFullYear() + num);
270
                return this;
271
        });
272
        
273
        /**
274
         * Add a number of months to the date object.
275
         * 
276
         * @example var dtm = new Date("01/12/2008");
277
         * dtm.addMonths(1);
278
         * dtm.toString();
279
         * @result 'Tue Feb 12 2008 00:00:00'
280
         * 
281
         * @name addMonths
282
         * @type Date
283
         * @cat Plugins/Methods/Date
284
         */
285
        add("addMonths", function(num) {
286
                var tmpdtm = this.getDate();
287
                
288
                this.setMonth(this.getMonth() + num);
289
                
290
                if (tmpdtm > this.getDate())
291
                        this.addDays(-this.getDate());
292
                
293
                return this;
294
        });
295
        
296
        /**
297
         * Add a number of days to the date object.
298
         * 
299
         * @example var dtm = new Date("01/12/2008");
300
         * dtm.addDays(1);
301
         * dtm.toString();
302
         * @result 'Sun Jan 13 2008 00:00:00'
303
         * 
304
         * @name addDays
305
         * @type Date
306
         * @cat Plugins/Methods/Date
307
         */
308
        add("addDays", function(num) {
309
                this.setDate(this.getDate() + num);
310
                return this;
311
        });
312
        
313
        /**
314
         * Add a number of hours to the date object.
315
         * 
316
         * @example var dtm = new Date("01/12/2008");
317
         * dtm.addHours(24);
318
         * dtm.toString();
319
         * @result 'Sun Jan 13 2008 00:00:00'
320
         * 
321
         * @name addHours
322
         * @type Date
323
         * @cat Plugins/Methods/Date
324
         */
325
        add("addHours", function(num) {
326
                this.setHours(this.getHours() + num);
327
                return this;
328
        });
329

    
330
        /**
331
         * Add a number of minutes to the date object.
332
         * 
333
         * @example var dtm = new Date("01/12/2008");
334
         * dtm.addMinutes(60);
335
         * dtm.toString();
336
         * @result 'Sat Jan 12 2008 01:00:00'
337
         * 
338
         * @name addMinutes
339
         * @type Date
340
         * @cat Plugins/Methods/Date
341
         */
342
        add("addMinutes", function(num) {
343
                this.setMinutes(this.getMinutes() + num);
344
                return this;
345
        });
346
        
347
        /**
348
         * Add a number of seconds to the date object.
349
         * 
350
         * @example var dtm = new Date("01/12/2008");
351
         * dtm.addSeconds(60);
352
         * dtm.toString();
353
         * @result 'Sat Jan 12 2008 00:01:00'
354
         * 
355
         * @name addSeconds
356
         * @type Date
357
         * @cat Plugins/Methods/Date
358
         */
359
        add("addSeconds", function(num) {
360
                this.setSeconds(this.getSeconds() + num);
361
                return this;
362
        });
363
        
364
        /**
365
         * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
366
         * 
367
         * @example var dtm = new Date();
368
         * dtm.zeroTime();
369
         * dtm.toString();
370
         * @result 'Sat Jan 12 2008 00:01:00'
371
         * 
372
         * @name zeroTime
373
         * @type Date
374
         * @cat Plugins/Methods/Date
375
         * @author Kelvin Luck
376
         */
377
        add("zeroTime", function() {
378
                this.setMilliseconds(0);
379
                this.setSeconds(0);
380
                this.setMinutes(0);
381
                this.setHours(0);
382
                return this;
383
        });
384
        
385
        /**
386
         * Returns a string representation of the date object according to Date.format.
387
         * (Date.toString may be used in other places so I purposefully didn't overwrite it)
388
         * 
389
         * @example var dtm = new Date("01/12/2008");
390
         * dtm.asString();
391
         * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
392
         * 
393
         * @name asString
394
         * @type Date
395
         * @cat Plugins/Methods/Date
396
         * @author Kelvin Luck
397
         */
398
        add("asString", function() {
399
                var r = Date.format;
400
                return r
401
                        .split('yyyy').join(this.getFullYear())
402
                        .split('yy').join(this.getYear())
403
                        .split('mmm').join(this.getMonthName(true))
404
                        .split('mm').join(_zeroPad(this.getMonth()+1))
405
                        .split('dd').join(_zeroPad(this.getDate()));
406
        });
407
        
408
        /**
409
         * Returns a new date object created from the passed String according to Date.format or false if the attempt to do this results in an invalid date object
410
         * (We can't simple use Date.parse as it's not aware of locale and I chose not to overwrite it incase it's functionality is being relied on elsewhere)
411
         *
412
         * @example var dtm = Date.fromString("12/01/2008");
413
         * dtm.toString();
414
         * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
415
         * 
416
         * @name fromString
417
         * @type Date
418
         * @cat Plugins/Methods/Date
419
         * @author Kelvin Luck
420
         */
421
        Date.fromString = function(s)
422
        {
423
                var f = Date.format;
424
                var d = new Date('01/01/1977');
425
                var iY = f.indexOf('yyyy');
426
                if (iY > -1) {
427
                        d.setFullYear(Number(s.substr(iY, 4)));
428
                } else {
429
                        // TODO - this doesn't work very well - are there any rules for what is meant by a two digit year?
430
                        d.setYear(Number(s.substr(f.indexOf('yy'), 2)));
431
                }
432
                var iM = f.indexOf('mmm');
433
                if (iM > -1) {
434
                        var mStr = s.substr(iM, 3);
435
                        for (var i=0; i<Date.abbrMonthNames.length; i++) {
436
                                if (Date.abbrMonthNames[i] == mStr) break;
437
                        }
438
                        d.setMonth(i);
439
                } else {
440
                        d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
441
                }
442
                d.setDate(Number(s.substr(f.indexOf('dd'), 2)));
443
                if (isNaN(d.getTime())) return false;
444
                return d;
445
        }
446
        
447
        // utility method
448
        var _zeroPad = function(num) {
449
                var s = '0'+num;
450
                return s.substring(s.length-2)
451
                //return ('0'+num).substring(-2); // doesn't work on IE :(
452
        };
453
        
454
})();