2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-18 20:10:36 +03:00

fix: when year < 100 && year >= 0 should be right

This commit is contained in:
mengxiong10
2019-11-13 15:30:07 +08:00
parent c03632fa87
commit 8c546cc994
4 changed files with 31 additions and 8 deletions
+13
View File
@@ -43,6 +43,19 @@ describe('CalendarPanel', () => {
expect(wrapper.emitted().select[0][0]).toEqual(new Date(2010, 0));
});
it('feat: when year >= 0 && year < 100, should be emit right', () => {
wrapper = mount(CalendarPanel, {
propsData: {
type: 'year',
defaultValue: new Date().setFullYear(11),
},
});
const tds = wrapper.findAll('.mx-table-year td > div');
tds.at(0).trigger('click');
const expectedDate = new Date(10, 0).setFullYear(10);
expect(wrapper.emitted().select[0][0].getTime()).toBe(expectedDate);
});
it('feat: active class', () => {
wrapper = mount(CalendarPanel);
const td = wrapper.find('.mx-table-date td:nth-child(6)');
+5 -5
View File
@@ -98,7 +98,7 @@ import {
} from 'date-fns';
import localeMixin from '../mixin/locale';
import formatMixin from '../mixin/format';
import { getValidDate, isValidDate } from '../util/date';
import { getValidDate, isValidDate, createDate } from '../util/date';
import TableDate from './table-date';
import TableMonth from './table-month';
import TableYear from './table-year';
@@ -114,7 +114,7 @@ export default {
props: {
value: {},
defaultValue: {
validator: value => isValidDate(value),
type: [Date, Number],
default() {
const date = new Date();
date.setHours(0, 0, 0, 0);
@@ -272,12 +272,12 @@ export default {
},
getCellDate(value, type) {
if (type === 'year') {
return new Date(value, 0);
return createDate(value, 0);
}
if (type === 'month') {
return new Date(this.calendarYear, value);
return createDate(this.calendarYear, value);
}
return new Date(this.calendarYear, this.calendarMonth, value);
return createDate(this.calendarYear, this.calendarMonth, value);
},
getDateClasses(day) {
const cellDate = this.getCellDate(day, 'date');
+4 -3
View File
@@ -31,6 +31,7 @@ import { getWeek } from 'date-format-parse';
import localeMixin from '../mixin/locale';
import formatMixin from '../mixin/format';
import { chunk } from '../util/base';
import { createDate } from '../util/date';
export default {
name: 'TableDate',
@@ -84,7 +85,7 @@ export default {
const month = this.calendarMonth;
// change to the last day of the last month
const calendar = new Date(year, month, 0);
const calendar = createDate(year, month, 0);
const lastDayInLastMonth = calendar.getDate();
// getDay() 0 is Sunday, 1 is Monday
const firstDayInLastMonth =
@@ -124,13 +125,13 @@ export default {
const year = this.calendarYear;
const month = this.calendarMonth;
const format = this.titleFormat;
const date = new Date(year, month, day);
const date = createDate(year, month, day);
return this.formatDate(date, format);
},
getWeekNumber(day) {
const year = this.calendarYear;
const month = this.calendarMonth;
const date = new Date(year, month, day);
const date = createDate(year, month, day);
return getWeek(date, this.t('formatLocale'));
},
},
+9
View File
@@ -1,3 +1,12 @@
// new Date(10, 0, 1) The year from 0 to 99 will be incremented by 1900 automatically.
export function createDate(y, M = 0, d = 1, h = 0, m = 0, s = 0, ms = 0) {
const date = new Date(y, M, d, h, m, s, ms);
if (y < 100 && y >= 0) {
date.setFullYear(y);
}
return date;
}
export function isValidDate(date) {
return date instanceof Date && !isNaN(date);
}