Skip to content

Commit

Permalink
fix(datetime-picker): incorrect value with 2 digits year format (#1175)
Browse files Browse the repository at this point in the history
* fix(datetime-picker): incorrect value with 2 digits year format

* test(datetime-picker): unit test to cover 2 digit year format

* fix(datetime-picker): remove 0 that use as reference point

* test(datetime-picker): add test to cover value change in 2 digit of year format
  • Loading branch information
bualoy-napat authored Jun 14, 2024
1 parent 0ab1230 commit 17b16c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,37 @@ describe('datetime-picker/Value', function () {
expect(el.timepickerFromEl.value).to.equal('11:00');
expect(el.timepickerToEl.value).to.equal('15:00');
});
it('value should format value correctly when it has a 2 digits of year format', async function () {
const el = await fixture(
'<ef-datetime-picker format="yy-MM-dd" lang="en-gb" opened></ef-datetime-picker>'
);
setTimeout(() => typeText(el.inputEl, '21-05-11'));
const {
detail: { value }
} = await oneEvent(el, 'value-changed');
await elementUpdated(el);
expect(el.value).to.be.equal('2021-05-11');
expect(el.calendarEl.value).to.be.equal('2021-05-11');
expect(value).to.be.equal('2021-05-11');
});
it('value should be formatted correctly when the user changes the year input while it has a 2 digit of year format', async function () {
const el = await fixture(
'<ef-datetime-picker format="yy-MM-dd" lang="en-gb" opened></ef-datetime-picker>'
);

el.value = '1450-12-12';

await elementUpdated(el);

setTimeout(() => typeText(el.inputEl, '51-12-12'));
const {
detail: { value }
} = await oneEvent(el, 'value-changed');
await elementUpdated(el);
expect(el.value).to.be.equal('1451-12-12');
expect(el.calendarEl.value).to.be.equal('1451-12-12');
expect(value).to.be.equal('1451-12-12');
});
// TODO: add input validation test cases when the value update is originated from typing input
});
});
2 changes: 1 addition & 1 deletion packages/elements/src/datetime-picker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DateTimeSegment {
* @returns {number} time
*/
public getTime(): number {
const date = this.dateSegment ? parse(this.dateSegment) : new Date(0);
const date = this.dateSegment ? parse(this.dateSegment) : new Date();
const timeSegment = toTimeSegment(this.timeSegment);
date.setHours(timeSegment.hours);
date.setMinutes(timeSegment.minutes);
Expand Down

0 comments on commit 17b16c2

Please sign in to comment.