Take leap years into account
This commit is contained in:
parent
454d7f10df
commit
a7aaec3cc2
@ -32,6 +32,8 @@ describe('DateSelector', () => {
|
|||||||
|
|
||||||
describe('daysMaxCalculation', () => {
|
describe('daysMaxCalculation', () => {
|
||||||
it('calculates correctly for each month', () => {
|
it('calculates correctly for each month', () => {
|
||||||
|
component.year = null
|
||||||
|
|
||||||
let months = {
|
let months = {
|
||||||
'1': 31,
|
'1': 31,
|
||||||
'2': 29,
|
'2': 29,
|
||||||
@ -52,6 +54,19 @@ describe('DateSelector', () => {
|
|||||||
expect(component.daysMaxCalculation).toEqual(months[month])
|
expect(component.daysMaxCalculation).toEqual(months[month])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('takes year or lack of year into account and calculates leap years', () => {
|
||||||
|
component.month = 2
|
||||||
|
|
||||||
|
component.year = null
|
||||||
|
expect(component.daysMaxCalculation).toEqual(29)
|
||||||
|
|
||||||
|
component.year = 2019
|
||||||
|
expect(component.daysMaxCalculation).toEqual(28)
|
||||||
|
|
||||||
|
component.year = 2016
|
||||||
|
expect(component.daysMaxCalculation).toEqual(29)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('isMonthValid', () => {
|
describe('isMonthValid', () => {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
|
import { getDaysInMonth } from 'date-fns'
|
||||||
|
|
||||||
var paddedNumber = function(number) {
|
var paddedNumber = function(number) {
|
||||||
if ((number + '').length === 1) {
|
if ((number + '').length === 1) {
|
||||||
@ -78,7 +79,11 @@ export default Vue.component('date-selector', {
|
|||||||
daysMaxCalculation: function() {
|
daysMaxCalculation: function() {
|
||||||
switch (parseInt(this.month)) {
|
switch (parseInt(this.month)) {
|
||||||
case 2: // February
|
case 2: // February
|
||||||
return 29
|
if (this.year) {
|
||||||
|
return getDaysInMonth(new Date(this.year, this.month - 1))
|
||||||
|
} else {
|
||||||
|
return 29
|
||||||
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
case 4: // April
|
case 4: // April
|
||||||
|
Loading…
x
Reference in New Issue
Block a user