2
* Copyright (C) 2013-2014 Canonical Ltd
4
* This file is part of Ubuntu Calendar App
6
* Ubuntu Calendar App is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 3 as
8
* published by the Free Software Foundation.
10
* Ubuntu Calendar App is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
Date.msPerDay = 86400e3
21
Date.msPerWeek = Date.msPerDay * 7
23
Date.leapYear = function(year) {
24
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
27
Date.daysInMonth = function(year, month) {
29
31/*Jan*/, 28/*Feb*/, 31/*Mar*/, 30/*Apr*/, 31/*May*/, 30/*Jun*/,
30
31/*Jul*/, 31/*Aug*/, 30/*Sep*/, 31/*Oct*/, 30/*Nov*/, 31/*Dec*/
31
][month] + (month == 1) * Date.leapYear(year)
34
Date.weeksInMonth = function(year, month, weekday) {
35
var y = year, m = month
36
var date0 = new Date(y, m, 1)
37
var date1 = new Date(y + (m == 11), m < 11 ? m + 1 : 0, 1)
38
var day = date0.getDay()
39
var m = (date1.getTime() - date0.getTime()) / Date.msPerDay
42
if (day == weekday) n = n + 1
43
day = day < 6 ? day + 1 : 0
49
Date.prototype.midnight = function() {
50
var date = new Date(this)
51
date.setHours(0,0,0,0);
55
Date.prototype.endOfDay = function() {
56
var date = new Date(this)
57
date.setHours(23,59,59,0);
61
Date.prototype.addDays = function(days) {
62
var date = new Date(this)
63
date.setDate(date.getDate() + days);
67
Date.prototype.addMonths = function(months) {
68
var date = new Date(this)
69
date.setMonth(date.getMonth() + months)
73
Date.prototype.weekStart = function(weekStartDay) {
74
var date = this.midnight()
75
var day = date.getDay(), n = 0
76
while (day != weekStartDay) {
81
return date.addDays(-n)
84
Date.prototype.monthStart = function() {
85
return this.midnight().addDays(1 - this.getDate())
88
Date.prototype.weekNumber = function() {
89
var date = this.weekStart(1).addDays(3) // Thursday midnight
90
var newYear = new Date(date.getFullYear(), 0 /*Jan*/, 1 /*the 1st*/)
92
var tx = date.getTime(), tn = newYear.getTime()
94
tx = tx - Date.msPerWeek
100
Date.prototype.weeksInMonth = function(weekday) {
101
return Date.weeksInMonth(this.getFullYear(), this.getMonth(), weekday)
104
Date.prototype.isSameDay = function ( otherDay ) {
105
return ( this.getDate() === otherDay.getDate()
106
&& this.getMonth() === otherDay.getMonth()
107
&& this.getFullYear() === otherDay.getFullYear() );
110
Date.prototype.mergeDate = function (d) {
111
this.setFullYear(d.getFullYear());
112
this.setMonth(d.getMonth());
113
this.setDate(d.getDate());
116
function weekCount(year, month_number) {
117
var firstOfMonth = new Date(year, month_number, 1);
118
var lastOfMonth = new Date(year, month_number+1, 0);
120
var used = firstOfMonth.getDay() + lastOfMonth.getDate();
122
return Math.ceil( used / 7);
125
function getFirstDateofWeek( year, month) {
126
var date = new Date(year, month, 1);
127
var first = date.getDate() - date.getDay();
128
return new Date(date.setDate(first));
132
var date = new Date();
133
date.setHours(0,0,0,0);
137
function isSameMonth(date1, date2) {
138
return ( date1.getFullYear() === date2.getFullYear()
139
&& date1.getMonth() === date2.getMonth() )