Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/js/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,12 @@ export class DateTime extends Date {
const y = year || dt.getFullYear();
let M = 0;
if (!(year && !month)) {
M = month > 0 ? month - 1 : dt.getMonth();
M =
month > 0
? localization.monthZeroIndex
? month
: month - 1
: dt.getMonth();
}
if (zone) {
return new DateTime(
Expand Down
6 changes: 5 additions & 1 deletion src/js/display/calendar/date-display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export default class DateDisplay {
// format the string to a date
const innerDate = DateTime.fromString(attributeValue, {
format: 'yyyy-MM-dd',
monthZeroIndex: true,
});

// find the position of the target in the date container
Expand All @@ -192,7 +193,7 @@ export default class DateDisplay {

//format the start date so that it can be found by the attribute
const rangeStartFormatted = this._dateToDataValue(rangeStart);
const rangeStartIndex = allDays.findIndex(
let rangeStartIndex = allDays.findIndex(
(e) => e.getAttribute('data-value') === rangeStartFormatted
);
const rangeStartElement = allDays[rangeStartIndex];
Expand All @@ -214,6 +215,9 @@ export default class DateDisplay {
let lambda: (_, index) => boolean;

if (innerDate.isBefore(rangeStart)) {
if (rangeStartElement === undefined) {
rangeStartIndex = Number.MAX_SAFE_INTEGER;
}
currentTarget.classList.add(Namespace.css.rangeStart);
rangeStartElement?.classList.remove(Namespace.css.rangeStart);
rangeStartElement?.classList.add(Namespace.css.rangeEnd);
Expand Down
1 change: 1 addition & 0 deletions src/js/utilities/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface FormatLocalization {
hourCycle?: Intl.LocaleHourCycleKey;
locale?: string;
ordinal?: (n: number) => any; //eslint-disable-line @typescript-eslint/no-explicit-any
monthZeroIndex?: boolean;
}

export interface Localization extends FormatLocalization {
Expand Down
16 changes: 16 additions & 0 deletions test/datetime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ test('Can create with string', () => {
expect(dt.month).toBe(12 - 1); //minus 1 because javascript 🙄
expect(dt.date).toBe(31);
expect(dt.year).toBe(2022);

const zeroIndexMonthDt = DateTime.fromString('2024-11-01', {
format: 'yyyy-MM-dd',
monthZeroIndex: true,
});
expect(zeroIndexMonthDt.year).toBe(2024);
expect(zeroIndexMonthDt.month).toBe(11);
expect(zeroIndexMonthDt.date).toBe(1);

const nonZeroIndexMonthDt = DateTime.fromString('2024-11-01', {
format: 'yyyy-MM-dd',
monthZeroIndex: false,
});
expect(nonZeroIndexMonthDt.year).toBe(2024);
expect(nonZeroIndexMonthDt.month).toBe(10);
expect(nonZeroIndexMonthDt.date).toBe(1);
});

test('Can create clone', () => {
Expand Down