Debian Codegraph @

src/imports/Components/1.2/dateUtils.js

raw
1 /*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 .pragma library
18
19 /*
20 Extending Date with few prototypes
21 */
22 Date.msPerDay = 86400e3
23 Date.msPerWeek = Date.msPerDay * 7
24
25 /*!
26 The function returns a Date object with the current date and hour set to
27 midnight.
28 */
29 Date.prototype.midnight = function() {
30 this.setHours(0, 0, 0, 0);
31 return this;
32 }
33
34 /*!
35 The function returns an invalid date object.
36 Example of use:
37 \code
38 var invalidDate = Date.prototype.getInvalidDate.call();
39 var otherInvalidDate = (new Date()).getInvalidDate();
40 \endcode
41 */
42 Date.prototype.getInvalidDate = function() {
43 return new Date(-1, -1);
44 }
45
46 /*!
47 The function checks whether the date object is a valid one, meaning the year,
48 month and date fields are positive numbers
49 */
50 Date.prototype.isValid = function() {
51 if (Object.prototype.toString.call(this) !== "[object Date]") {
52 return false;
53 }
54 return (this.getFullYear() > 0) && (this.getMonth() >= 0) && (this.getDate() > 0);
55 }
56
57 /*!
58 The function returns the number of days in the month set in the Date object.
59 */
60 Date.prototype.daysInMonth = function() {
61 return [
62 31/*an*/, 28/*Feb*/, 31/*Mar*/, 30/*Apr*/, 31/*May*/, 30/*Jun*/,
63 31/*Jul*/, 31/*Aug*/, 30/*Sep*/, 31/*Oct*/, 30/*Nov*/, 31/*Dec*/
64 ][this.getMonth()] + (this.getMonth() === 1) * this.leapYear();
65 }
66
67 /*!
68 The function checks whether the year in the Date object is a leap year or not.
69 */
70 Date.prototype.leapYear = function() {
71 var year = this.getFullYear();
72 return year % 400 == 0 || (year % 100 !== 0 && year % 4 == 0);
73 }
74
75 /*!
76 The function returns the distance in months (not calendaristic months) between
77 the Date object and the given one as parameter.
78 */
79 Date.prototype.monthsTo = function(target) {
80 return target.getMonth() - this.getMonth() + (12 * (target.getFullYear() - this.getFullYear()));
81 }
82
83 /*!
84 Same as monthsTo, but returns the distance in days.
85 */
86 Date.prototype.daysTo = function(target) {
87 return !target.isValid() ? 0 : Math.ceil((target - this) / Date.msPerDay);
88 }
89
90 /*!
91 Same as monthsTo, but returns the distance in hours.
92 */
93 Date.prototype.hoursTo = function(target) {
94 return !target.isValid() ? 0 : Math.ceil((target.getTime() - this.getTime()) / (1000 * 60 * 60));
95 }
96
97 /*!
98 Same as monthsTo, but returns the distance in minutes.
99 */
100 Date.prototype.minutesTo = function(target) {
101 return !target.isValid() ? 0 : Math.ceil((target.getTime() - this.getTime()) / (1000 * 60));
102 }
103
104 /*!
105 Same as monthsTo, but returns the distance in seconds.
106 */
107 Date.prototype.secondsTo = function(target) {
108 return !target.isValid() ? 0 : Math.ceil((target.getTime() - this.getTime()) / 1000);
109 }
110
111 /*!
112 The function returns the week number of the date stored in the object.
113 */
114 Date.prototype.getWeek = function() {
115 // Copy date so don't modify original
116 var date = new Date(this);
117 date.setHours(0, 0, 0, 0);
118 // Set to nearest Thursday: current date + 4 - current day number
119 // Make Sunday's day number 7
120 date.setDate(date.getDate() + 4 - (date.getDay() || 7));
121 // Get first day of year
122 var yearStart = new Date(date.getFullYear(), 0, 1);
123 // Calculate full weeks to nearest Thursday
124 var weekNo = Math.ceil((((date - yearStart) / 86400000) + 1) / 7);
125 // Return array of year and week number
126 return weekNo;
127 }