~lss-team/lilsoftstats/trunk

« back to all changes in this revision

Viewing changes to js/jquery/date.js

  • Committer: Nick
  • Date: 2011-11-14 04:10:28 UTC
  • Revision ID: nick@little-apps.org-20111114041028-cvmpwq6z6hx3pkya
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/**
 
92
 * The first two numbers in the century to be used when decoding a two digit year. Since a two digit year is ambiguous (and date.setYear
 
93
 * only works with numbers < 99 and so doesn't allow you to set years after 2000) we need to use this to disambiguate the two digit year codes.
 
94
 *
 
95
 * @name format
 
96
 * @type String
 
97
 * @cat Plugins/Methods/Date
 
98
 * @author Kelvin Luck
 
99
 */
 
100
Date.fullYearStart = '20';
 
101
 
 
102
(function() {
 
103
 
 
104
        /**
 
105
         * Adds a given method under the given name 
 
106
         * to the Date prototype if it doesn't
 
107
         * currently exist.
 
108
         *
 
109
         * @private
 
110
         */
 
111
        function add(name, method) {
 
112
                if( !Date.prototype[name] ) {
 
113
                        Date.prototype[name] = method;
 
114
                }
 
115
        };
 
116
        
 
117
        /**
 
118
         * Checks if the year is a leap year.
 
119
         *
 
120
         * @example var dtm = new Date("01/12/2008");
 
121
         * dtm.isLeapYear();
 
122
         * @result true
 
123
         *
 
124
         * @name isLeapYear
 
125
         * @type Boolean
 
126
         * @cat Plugins/Methods/Date
 
127
         */
 
128
        add("isLeapYear", function() {
 
129
                var y = this.getFullYear();
 
130
                return (y%4==0 && y%100!=0) || y%400==0;
 
131
        });
 
132
        
 
133
        /**
 
134
         * Checks if the day is a weekend day (Sat or Sun).
 
135
         *
 
136
         * @example var dtm = new Date("01/12/2008");
 
137
         * dtm.isWeekend();
 
138
         * @result false
 
139
         *
 
140
         * @name isWeekend
 
141
         * @type Boolean
 
142
         * @cat Plugins/Methods/Date
 
143
         */
 
144
        add("isWeekend", function() {
 
145
                return this.getDay()==0 || this.getDay()==6;
 
146
        });
 
147
        
 
148
        /**
 
149
         * Check if the day is a day of the week (Mon-Fri)
 
150
         * 
 
151
         * @example var dtm = new Date("01/12/2008");
 
152
         * dtm.isWeekDay();
 
153
         * @result false
 
154
         * 
 
155
         * @name isWeekDay
 
156
         * @type Boolean
 
157
         * @cat Plugins/Methods/Date
 
158
         */
 
159
        add("isWeekDay", function() {
 
160
                return !this.isWeekend();
 
161
        });
 
162
        
 
163
        /**
 
164
         * Gets the number of days in the month.
 
165
         * 
 
166
         * @example var dtm = new Date("01/12/2008");
 
167
         * dtm.getDaysInMonth();
 
168
         * @result 31
 
169
         * 
 
170
         * @name getDaysInMonth
 
171
         * @type Number
 
172
         * @cat Plugins/Methods/Date
 
173
         */
 
174
        add("getDaysInMonth", function() {
 
175
                return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
 
176
        });
 
177
        
 
178
        /**
 
179
         * Gets the name of the day.
 
180
         * 
 
181
         * @example var dtm = new Date("01/12/2008");
 
182
         * dtm.getDayName();
 
183
         * @result 'Saturday'
 
184
         * 
 
185
         * @example var dtm = new Date("01/12/2008");
 
186
         * dtm.getDayName(true);
 
187
         * @result 'Sat'
 
188
         * 
 
189
         * @param abbreviated Boolean When set to true the name will be abbreviated.
 
190
         * @name getDayName
 
191
         * @type String
 
192
         * @cat Plugins/Methods/Date
 
193
         */
 
194
        add("getDayName", function(abbreviated) {
 
195
                return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
 
196
        });
 
197
 
 
198
        /**
 
199
         * Gets the name of the month.
 
200
         * 
 
201
         * @example var dtm = new Date("01/12/2008");
 
202
         * dtm.getMonthName();
 
203
         * @result 'Janurary'
 
204
         *
 
205
         * @example var dtm = new Date("01/12/2008");
 
206
         * dtm.getMonthName(true);
 
207
         * @result 'Jan'
 
208
         * 
 
209
         * @param abbreviated Boolean When set to true the name will be abbreviated.
 
210
         * @name getDayName
 
211
         * @type String
 
212
         * @cat Plugins/Methods/Date
 
213
         */
 
214
        add("getMonthName", function(abbreviated) {
 
215
                return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
 
216
        });
 
217
 
 
218
        /**
 
219
         * Get the number of the day of the year.
 
220
         * 
 
221
         * @example var dtm = new Date("01/12/2008");
 
222
         * dtm.getDayOfYear();
 
223
         * @result 11
 
224
         * 
 
225
         * @name getDayOfYear
 
226
         * @type Number
 
227
         * @cat Plugins/Methods/Date
 
228
         */
 
229
        add("getDayOfYear", function() {
 
230
                var tmpdtm = new Date("1/1/" + this.getFullYear());
 
231
                return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
 
232
        });
 
233
        
 
234
        /**
 
235
         * Get the number of the week of the year.
 
236
         * 
 
237
         * @example var dtm = new Date("01/12/2008");
 
238
         * dtm.getWeekOfYear();
 
239
         * @result 2
 
240
         * 
 
241
         * @name getWeekOfYear
 
242
         * @type Number
 
243
         * @cat Plugins/Methods/Date
 
244
         */
 
245
        add("getWeekOfYear", function() {
 
246
                return Math.ceil(this.getDayOfYear() / 7);
 
247
        });
 
248
 
 
249
        /**
 
250
         * Set the day of the year.
 
251
         * 
 
252
         * @example var dtm = new Date("01/12/2008");
 
253
         * dtm.setDayOfYear(1);
 
254
         * dtm.toString();
 
255
         * @result 'Tue Jan 01 2008 00:00:00'
 
256
         * 
 
257
         * @name setDayOfYear
 
258
         * @type Date
 
259
         * @cat Plugins/Methods/Date
 
260
         */
 
261
        add("setDayOfYear", function(day) {
 
262
                this.setMonth(0);
 
263
                this.setDate(day);
 
264
                return this;
 
265
        });
 
266
        
 
267
        /**
 
268
         * Add a number of years to the date object.
 
269
         * 
 
270
         * @example var dtm = new Date("01/12/2008");
 
271
         * dtm.addYears(1);
 
272
         * dtm.toString();
 
273
         * @result 'Mon Jan 12 2009 00:00:00'
 
274
         * 
 
275
         * @name addYears
 
276
         * @type Date
 
277
         * @cat Plugins/Methods/Date
 
278
         */
 
279
        add("addYears", function(num) {
 
280
                this.setFullYear(this.getFullYear() + num);
 
281
                return this;
 
282
        });
 
283
        
 
284
        /**
 
285
         * Add a number of months to the date object.
 
286
         * 
 
287
         * @example var dtm = new Date("01/12/2008");
 
288
         * dtm.addMonths(1);
 
289
         * dtm.toString();
 
290
         * @result 'Tue Feb 12 2008 00:00:00'
 
291
         * 
 
292
         * @name addMonths
 
293
         * @type Date
 
294
         * @cat Plugins/Methods/Date
 
295
         */
 
296
        add("addMonths", function(num) {
 
297
                var tmpdtm = this.getDate();
 
298
                
 
299
                this.setMonth(this.getMonth() + num);
 
300
                
 
301
                if (tmpdtm > this.getDate())
 
302
                        this.addDays(-this.getDate());
 
303
                
 
304
                return this;
 
305
        });
 
306
        
 
307
        /**
 
308
         * Add a number of days to the date object.
 
309
         * 
 
310
         * @example var dtm = new Date("01/12/2008");
 
311
         * dtm.addDays(1);
 
312
         * dtm.toString();
 
313
         * @result 'Sun Jan 13 2008 00:00:00'
 
314
         * 
 
315
         * @name addDays
 
316
         * @type Date
 
317
         * @cat Plugins/Methods/Date
 
318
         */
 
319
        add("addDays", function(num) {
 
320
                //this.setDate(this.getDate() + num);
 
321
                this.setTime(this.getTime() + (num*86400000) );
 
322
                return this;
 
323
        });
 
324
        
 
325
        /**
 
326
         * Add a number of hours to the date object.
 
327
         * 
 
328
         * @example var dtm = new Date("01/12/2008");
 
329
         * dtm.addHours(24);
 
330
         * dtm.toString();
 
331
         * @result 'Sun Jan 13 2008 00:00:00'
 
332
         * 
 
333
         * @name addHours
 
334
         * @type Date
 
335
         * @cat Plugins/Methods/Date
 
336
         */
 
337
        add("addHours", function(num) {
 
338
                this.setHours(this.getHours() + num);
 
339
                return this;
 
340
        });
 
341
 
 
342
        /**
 
343
         * Add a number of minutes to the date object.
 
344
         * 
 
345
         * @example var dtm = new Date("01/12/2008");
 
346
         * dtm.addMinutes(60);
 
347
         * dtm.toString();
 
348
         * @result 'Sat Jan 12 2008 01:00:00'
 
349
         * 
 
350
         * @name addMinutes
 
351
         * @type Date
 
352
         * @cat Plugins/Methods/Date
 
353
         */
 
354
        add("addMinutes", function(num) {
 
355
                this.setMinutes(this.getMinutes() + num);
 
356
                return this;
 
357
        });
 
358
        
 
359
        /**
 
360
         * Add a number of seconds to the date object.
 
361
         * 
 
362
         * @example var dtm = new Date("01/12/2008");
 
363
         * dtm.addSeconds(60);
 
364
         * dtm.toString();
 
365
         * @result 'Sat Jan 12 2008 00:01:00'
 
366
         * 
 
367
         * @name addSeconds
 
368
         * @type Date
 
369
         * @cat Plugins/Methods/Date
 
370
         */
 
371
        add("addSeconds", function(num) {
 
372
                this.setSeconds(this.getSeconds() + num);
 
373
                return this;
 
374
        });
 
375
        
 
376
        /**
 
377
         * Sets the time component of this Date to zero for cleaner, easier comparison of dates where time is not relevant.
 
378
         * 
 
379
         * @example var dtm = new Date();
 
380
         * dtm.zeroTime();
 
381
         * dtm.toString();
 
382
         * @result 'Sat Jan 12 2008 00:01:00'
 
383
         * 
 
384
         * @name zeroTime
 
385
         * @type Date
 
386
         * @cat Plugins/Methods/Date
 
387
         * @author Kelvin Luck
 
388
         */
 
389
        add("zeroTime", function() {
 
390
                this.setMilliseconds(0);
 
391
                this.setSeconds(0);
 
392
                this.setMinutes(0);
 
393
                this.setHours(0);
 
394
                return this;
 
395
        });
 
396
        
 
397
        /**
 
398
         * Returns a string representation of the date object according to Date.format.
 
399
         * (Date.toString may be used in other places so I purposefully didn't overwrite it)
 
400
         * 
 
401
         * @example var dtm = new Date("01/12/2008");
 
402
         * dtm.asString();
 
403
         * @result '12/01/2008' // (where Date.format == 'dd/mm/yyyy'
 
404
         * 
 
405
         * @name asString
 
406
         * @type Date
 
407
         * @cat Plugins/Methods/Date
 
408
         * @author Kelvin Luck
 
409
         */
 
410
        add("asString", function(format) {
 
411
                var r = format || Date.format;
 
412
                if (r.split('mm').length>1) { // ugly workaround to make sure we don't replace the m's in e.g. noveMber
 
413
                        r = r.split('mmmm').join(this.getMonthName(false))
 
414
                                .split('mmm').join(this.getMonthName(true))
 
415
                                .split('mm').join(_zeroPad(this.getMonth()+1))
 
416
                } else {
 
417
                        r = r.split('m').join(this.getMonth()+1);
 
418
                }
 
419
                r = r.split('yyyy').join(this.getFullYear())
 
420
                        .split('yy').join((this.getFullYear() + '').substring(2))
 
421
                        .split('dd').join(_zeroPad(this.getDate()))
 
422
                        .split('d').join(this.getDate());
 
423
                return r;
 
424
        });
 
425
        
 
426
        /**
 
427
         * 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
 
428
         * (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)
 
429
         *
 
430
         * @example var dtm = Date.fromString("12/01/2008");
 
431
         * dtm.toString();
 
432
         * @result 'Sat Jan 12 2008 00:00:00' // (where Date.format == 'dd/mm/yyyy'
 
433
         * 
 
434
         * @name fromString
 
435
         * @type Date
 
436
         * @cat Plugins/Methods/Date
 
437
         * @author Kelvin Luck
 
438
         */
 
439
        Date.fromString = function(s)
 
440
        {
 
441
                var f = Date.format;
 
442
                
 
443
                var d = new Date('01/01/1970');
 
444
                
 
445
                if (s == '') return d;
 
446
 
 
447
                s = s.toLowerCase();
 
448
                var matcher = '';
 
449
                var order = [];
 
450
                var r = /(dd?d?|mm?m?|yy?yy?)+([^(m|d|y)])?/g;
 
451
                var results;
 
452
                while ((results = r.exec(f)) != null)
 
453
                {
 
454
                        switch (results[1]) {
 
455
                                case 'd':
 
456
                                case 'dd':
 
457
                                case 'm':
 
458
                                case 'mm':
 
459
                                case 'yy':
 
460
                                case 'yyyy':
 
461
                                        matcher += '(\\d+\\d?\\d?\\d?)+';
 
462
                                        order.push(results[1].substr(0, 1));
 
463
                                        break;
 
464
                                case 'mmm':
 
465
                                        matcher += '([a-z]{3})';
 
466
                                        order.push('M');
 
467
                                        break;
 
468
                        }
 
469
                        if (results[2]) {
 
470
                                matcher += results[2];
 
471
                        }
 
472
                        
 
473
                }
 
474
                var dm = new RegExp(matcher);
 
475
                var result = s.match(dm);
 
476
                for (var i=0; i<order.length; i++) {
 
477
                        var res = result[i+1];
 
478
                        switch(order[i]) {
 
479
                                case 'd':
 
480
                                        d.setDate(res);
 
481
                                        break;
 
482
                                case 'm':
 
483
                                        d.setMonth(Number(res)-1);
 
484
                                        break;
 
485
                                case 'M':
 
486
                                        for (var j=0; j<Date.abbrMonthNames.length; j++) {
 
487
                                                if (Date.abbrMonthNames[j].toLowerCase() == res) break;
 
488
                                        }
 
489
                                        d.setMonth(j);
 
490
                                        break;
 
491
                                case 'y':
 
492
                                        d.setYear(res);
 
493
                                        break;
 
494
                        }
 
495
                }
 
496
 
 
497
                return d;
 
498
        };
 
499
        
 
500
        // utility method
 
501
        var _zeroPad = function(num) {
 
502
                var s = '0'+num;
 
503
                return s.substring(s.length-2)
 
504
                //return ('0'+num).substring(-2); // doesn't work on IE :(
 
505
        };
 
506
        
 
507
})();
 
 
b'\\ No newline at end of file'