Skip to content

Commit 39f6138

Browse files
committed
fix: [QDateTime] Month 'forward' button has undesirable behaviour quasarframework#847
Also fixed glitch in "date" utils: adjustDate, addToDate & subtractFromDate methods
1 parent 8f819ce commit 39f6138

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/components/datetime/InlineDatetimeIOS.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
import { between, capitalize } from '../../utils/format'
8989
import { position } from '../../utils/event'
9090
import { css } from '../../utils/dom'
91-
import { isSameDate } from '../../utils/date'
91+
import { isSameDate, adjustDate } from '../../utils/date'
9292
import DateMixin from './datetime-mixin'
9393
import TouchPan from '../../directives/touch-pan'
9494
@@ -197,7 +197,7 @@ export default {
197197
},
198198
setMonth (value) {
199199
if (this.editable) {
200-
this.model = new Date(this.model.setMonth(this.__parseTypeValue('month', value) - 1))
200+
this.model = adjustDate(this.model, {month: value})
201201
}
202202
},
203203
setDay (value) {

src/components/datetime/InlineDatetimeMat.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
small
107107
flat
108108
:color="color"
109-
@click="setMonth(month - 1, true)"
109+
@click="setMonth(month - 1)"
110110
:disabled="beforeMinDays"
111111
icon="keyboard_arrow_left"
112112
></q-btn>
@@ -118,7 +118,7 @@
118118
small
119119
flat
120120
:color="color"
121-
@click="setMonth(month + 1, true)"
121+
@click="setMonth(month + 1)"
122122
:disabled="afterMaxDays"
123123
icon="keyboard_arrow_right"
124124
></q-btn>
@@ -222,7 +222,7 @@
222222
import { height, width, offset, cssTransform } from '../../utils/dom'
223223
import { position } from '../../utils/event'
224224
import { QBtn } from '../btn'
225-
import { isSameDate } from '../../utils/date'
225+
import { isSameDate, adjustDate } from '../../utils/date'
226226
import DateMixin from './datetime-mixin'
227227
import Ripple from '../../directives/ripple'
228228
@@ -383,10 +383,10 @@ export default {
383383
this.model = new Date(this.model.setFullYear(this.__parseTypeValue('year', value)))
384384
}
385385
},
386-
setMonth (value, force) {
386+
setMonth (value) {
387387
if (this.editable) {
388388
this.view = 'day'
389-
this.model = new Date(this.model.setMonth((force ? value : this.__parseTypeValue('month', value)) - 1))
389+
this.model = adjustDate(this.model, {month: value})
390390
}
391391
},
392392
setDay (value) {

src/utils/date.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,28 @@ function formatTimezone (offset, delimeter = '') {
2828
return sign + pad(hours) + delimeter + pad(minutes)
2929
}
3030

31+
function setMonth (date, newMonth /* 1-based */) {
32+
const
33+
test = new Date(date.getFullYear(), newMonth, 0, 0, 0, 0, 0),
34+
days = test.getDate()
35+
36+
date.setMonth(newMonth - 1, Math.min(days, date.getDate()))
37+
}
38+
3139
function getChange (date, mod, add) {
3240
const
3341
t = new Date(date),
3442
sign = (add ? 1 : -1)
3543

3644
Object.keys(mod).forEach(key => {
37-
const op = capitalize(key === 'days' ? 'date' : key)
45+
if (key === 'month') {
46+
setMonth(t, t.getMonth() + 1 + sign * mod.month)
47+
return
48+
}
49+
50+
const op = key === 'year'
51+
? 'FullYear'
52+
: capitalize(key === 'days' ? 'date' : key)
3853
t[`set${op}`](t[`get${op}`]() + sign * mod[key])
3954
})
4055
return t
@@ -98,6 +113,11 @@ export function adjustDate (date, mod, utc) {
98113
prefix = `set${utc ? 'UTC' : ''}`
99114

100115
Object.keys(mod).forEach(key => {
116+
if (key === 'month') {
117+
setMonth(t, mod.month)
118+
return
119+
}
120+
101121
const op = key === 'year'
102122
? 'FullYear'
103123
: key.charAt(0).toUpperCase() + key.slice(1)

0 commit comments

Comments
 (0)