~mihirsoni/ubuntu-calendar-app/dateSelectNewEventTest

« back to all changes in this revision

Viewing changes to dateExt.js

  • Committer: Kunal Parmar
  • Date: 2014-10-20 15:20:21 UTC
  • mto: This revision was merged to the branch mainline in revision 510.
  • Revision ID: pkunal.parmar@gmail.com-20141020152021-e2fv7dqqu4zv4g2y
defect resolved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013-2014 Canonical Ltd
 
3
 *
 
4
 * This file is part of Ubuntu Calendar App
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
.pragma library
 
19
 
 
20
Date.msPerDay = 86400e3
 
21
Date.msPerWeek = Date.msPerDay * 7
 
22
 
 
23
Date.leapYear = function(year) {
 
24
    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
 
25
}
 
26
 
 
27
Date.daysInMonth = function(year, month) {
 
28
    return [
 
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)
 
32
}
 
33
 
 
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
 
40
    var n = 0
 
41
    while (m > 0) {
 
42
        if (day == weekday) n = n + 1
 
43
        day = day < 6 ? day + 1 : 0
 
44
        m = m - 1
 
45
    }
 
46
    return n
 
47
}
 
48
 
 
49
Date.prototype.midnight = function() {
 
50
    var date = new Date(this)
 
51
    date.setHours(0,0,0,0);
 
52
    return date
 
53
}
 
54
 
 
55
Date.prototype.endOfDay = function() {
 
56
    var date = new Date(this)
 
57
    date.setHours(23,59,59,0);
 
58
    return date
 
59
}
 
60
 
 
61
Date.prototype.addDays = function(days) {
 
62
    var date = new Date(this)
 
63
    date.setDate(date.getDate() + days);
 
64
    return date
 
65
}
 
66
 
 
67
Date.prototype.addMonths = function(months) {
 
68
    var date = new Date(this)
 
69
    date.setMonth(date.getMonth() + months)
 
70
    return date
 
71
}
 
72
 
 
73
Date.prototype.weekStart = function(weekStartDay) {
 
74
    var date = this.midnight()
 
75
    var day = date.getDay(), n = 0
 
76
    while (day != weekStartDay) {
 
77
        if (day == 0) day = 6
 
78
        else day = day - 1
 
79
        n = n + 1
 
80
    }
 
81
    return date.addDays(-n)
 
82
}
 
83
 
 
84
Date.prototype.monthStart = function() {
 
85
    return this.midnight().addDays(1 - this.getDate())
 
86
}
 
87
 
 
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*/)
 
91
    var n = 0
 
92
    var tx = date.getTime(), tn = newYear.getTime()
 
93
    while (tn < tx) {
 
94
        tx = tx - Date.msPerWeek
 
95
        n = n + 1
 
96
    }
 
97
    return n
 
98
}
 
99
 
 
100
Date.prototype.weeksInMonth = function(weekday) {
 
101
    return Date.weeksInMonth(this.getFullYear(), this.getMonth(), weekday)
 
102
}
 
103
 
 
104
Date.prototype.isSameDay = function ( otherDay ) {
 
105
    return ( this.getDate() === otherDay.getDate()
 
106
    && this.getMonth() === otherDay.getMonth()
 
107
    && this.getFullYear() === otherDay.getFullYear() );
 
108
}
 
109
 
 
110
Date.prototype.mergeDate = function (d) {
 
111
    this.setFullYear(d.getFullYear());
 
112
    this.setMonth(d.getMonth());
 
113
    this.setDate(d.getDate());
 
114
}
 
115
 
 
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);
 
119
 
 
120
    var used = firstOfMonth.getDay() + lastOfMonth.getDate();
 
121
 
 
122
    return Math.ceil( used / 7);
 
123
}
 
124
 
 
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));
 
129
}
 
130
 
 
131
function today() {
 
132
    var date = new Date();
 
133
    date.setHours(0,0,0,0);
 
134
    return date
 
135
}
 
136
 
 
137
function isSameMonth(date1, date2) {
 
138
    return ( date1.getFullYear() === date2.getFullYear()
 
139
            && date1.getMonth() === date2.getMonth() )
 
140
}