Skip to content

Commit fe72ebd

Browse files
committed
fix(calendar): generated dates are normalized
1 parent 97143ca commit fe72ebd

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

packages/calendar/src/LionCalendar.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { isSameDate } from './utils/isSameDate.js';
99
import { calendarStyle } from './calendarStyle.js';
1010
import './utils/differentKeyNamesShimIE.js';
1111
import { createDay } from './utils/createDay.js';
12+
import { normalizeDateTime } from './utils/normalizeDateTime.js';
1213

1314
/**
1415
* @customElement
@@ -152,7 +153,7 @@ export class LionCalendar extends LocalizeMixin(LitElement) {
152153
this.disableDates = () => false;
153154
this.firstDayOfWeek = 0;
154155
this.weekdayHeaderNotation = 'short';
155-
this.__today = new Date();
156+
this.__today = normalizeDateTime(new Date());
156157
this.centralDate = this.__today;
157158
this.__focusedDate = null;
158159
this.__connectedCallbackDone = false;
@@ -344,6 +345,7 @@ export class LionCalendar extends LocalizeMixin(LitElement) {
344345
if (this.minDate && day.date < this.minDate) {
345346
day.disabled = true;
346347
}
348+
347349
if (this.maxDate && day.date > this.maxDate) {
348350
day.disabled = true;
349351
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
/**
3+
* @desc Makes suitable for date comparisons
4+
* @param {Date} d
5+
* @returns {Date}
6+
*/
7+
export function normalizeDateTime(d) {
8+
return new Date(d.getFullYear(), d.getMonth(), d.getDate());
9+
}

packages/calendar/test/lion-calendar.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ describe('<lion-calendar>', () => {
338338

339339
clock.restore();
340340
});
341+
342+
describe('Normalization', () => {
343+
it('normalizes all generated dates', async () => {
344+
function isNormalizedDate(d) {
345+
return d.getHours() === 0 && d.getMinutes() === 0 && d.getSeconds() === 0;
346+
}
347+
348+
const el = await fixture(
349+
html`
350+
<lion-calendar></lion-calendar>
351+
`,
352+
);
353+
// The central date will be today's date: it's the date all other
354+
// dates in the month view will be derived from.
355+
expect(isNormalizedDate(el.centralDate)).to.be.true;
356+
});
357+
});
341358
});
342359
});
343360

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { expect } from '@open-wc/testing';
2+
import { normalizeDateTime } from '../../src/utils/normalizeDateTime.js';
3+
4+
describe('normalizeDateTime', () => {
5+
it('returns a date with hours, minutes and seconds set to 0', () => {
6+
const date = normalizeDateTime(new Date('2000-11-29T12:34:56'));
7+
8+
expect(date.getFullYear()).to.equal(2000);
9+
expect(date.getMonth()).to.equal(10);
10+
expect(date.getDate()).to.equal(29);
11+
// normalized parts
12+
expect(date.getHours()).to.equal(0);
13+
expect(date.getMinutes()).to.equal(0);
14+
expect(date.getSeconds()).to.equal(0);
15+
});
16+
});

0 commit comments

Comments
 (0)