Skip to content

Commit

Permalink
#2693 fixes customDateFormat undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
Eonasdan committed Oct 31, 2022
1 parent 6886dda commit 5043731
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1,121 deletions.
54 changes: 38 additions & 16 deletions dist/plugins/customDateFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
L: 'MM/dd/yyyy',
LL: 'MMMM d, yyyy',
LLL: 'MMMM d, yyyy h:mm T',
LLLL: 'dddd, MMMM d, yyyy h:mm T',
LLLL: 'dddd, MMMM d, yyyy h:mm T'
};
this.formattingTokens = /(\[[^[]*])|([-_:/.,()\s]+)|(T|t|yyyy|yy?|MM?M?M?|Do|dd?|hh?|HH?|mm?|ss?|z|ZZ?)/g;
this.formattingTokens = /(\[[^[]*])|([-_:/.,()\s]+)|(T|t|yyyy|yy?|MM?M?M?|Do|dd?|hh?|HH?|mm?|ss?|z|zz?z?)/g;
this.match1 = /\d/; // 0 - 9
this.match2 = /\d\d/; // 00 - 99
this.match3 = /\d{3}/; // 000 - 999
Expand All @@ -28,26 +28,26 @@
this.matchOffset,
(obj, input) => {
obj.offset = this.offsetFromString(input);
},
}
];
this.expressions = {
t: [
this.matchWord,
(ojb, input) => {
ojb.afternoon = this.meridiemMatch(input);
},
}
],
T: [
this.matchWord,
(ojb, input) => {
ojb.afternoon = this.meridiemMatch(input);
},
}
],
fff: [
this.match3,
(ojb, input) => {
ojb.milliseconds = +input;
},
}
],
s: [this.match1to2, this.addInput('seconds')],
ss: [this.match1to2, this.addInput('seconds')],
Expand All @@ -70,7 +70,7 @@
ojb.day = i;
}
}
},
}
],
M: [this.match1to2, this.addInput('month')],
MM: [this.match2, this.addInput('month')],
Expand All @@ -84,7 +84,7 @@
throw new Error();
}
obj.month = matchIndex % 12 || matchIndex;
},
}
],
MMMM: [
this.matchWord,
Expand All @@ -95,18 +95,19 @@
throw new Error();
}
obj.month = matchIndex % 12 || matchIndex;
},
}
],
y: [this.matchSigned, this.addInput('year')],
yy: [
this.match2,
(obj, input) => {
obj.year = this.parseTwoDigitYear(input);
},
}
],
yyyy: [this.match4, this.addInput('year')],
Z: this.zoneExpressions,
ZZ: this.zoneExpressions,
// z: this.zoneExpressions,
// zz: this.zoneExpressions,
// zzz: this.zoneExpressions
};
this.parseFormattedInput = (input) => {
if (!this.localization.format) {
Expand Down Expand Up @@ -174,10 +175,29 @@
};
}
;
/**
* z = -4, zz = -04, zzz = -0400
* @param date
* @param style
* @private
*/
zoneInformation(date, style) {
let name = date.parts(this.localization.locale, { timeZoneName: 'longOffset' })
.timeZoneName
.replace('GMT', '')
.replace(':', '');
let negative = name.includes('-');
name = name.replace('-');
if (style === 'z')
name = name.substring(1, 2);
else if (style === 'zz')
name = name.substring(0, 2);
return `${negative ? '-' : ''}${name}`;
}
meridiemMatch(input) {
const meridiem = new Intl.DateTimeFormat(this.localization.locale, {
hour: 'numeric',
hour12: true,
hour12: true
})
.formatToParts(new Date(2022, 3, 4, 13))
.find((p) => p.type === 'dayPeriod')?.value;
Expand Down Expand Up @@ -251,8 +271,8 @@
MMMM: this.getAllMonths()[dateTime.getMonth()],
d: dateTime.date,
dd: dateTime.dateFormatted,
ddd: formatter({ weekday: "short" }),
dddd: formatter({ weekday: "long" }),
ddd: formatter({ weekday: 'short' }),
dddd: formatter({ weekday: 'long' }),
H: dateTime.getHours(),
HH: dateTime.hoursFormatted,
h: dateTime.hours > 12 ? dateTime.hours - 12 : dateTime.hours,
Expand All @@ -264,7 +284,9 @@
s: dateTime.seconds,
ss: dateTime.secondsFormatted,
fff: dateTime.getMilliseconds(),
//z: dateTime.getTimezoneOffset() todo zones are stupid
// z: this.zoneInformation(dateTime, 'z'), //-4
// zz: this.zoneInformation(dateTime, 'zz'), //-04
// zzz: this.zoneInformation(dateTime, 'zzz') //-0400
};
return format.replace(this.REGEX_FORMAT, (match, $1) => {
return $1 || matches[match];
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/customDateFormat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ class CustomDateFormat {
}
],
yyyy: [this.match4, this.addInput('year')],
z: this.zoneExpressions,
zz: this.zoneExpressions,
zzz: this.zoneExpressions
// z: this.zoneExpressions,
// zz: this.zoneExpressions,
// zzz: this.zoneExpressions
};

private correctHours(time) {
Expand Down Expand Up @@ -304,9 +304,9 @@ class CustomDateFormat {
s: dateTime.seconds,
ss: dateTime.secondsFormatted,
fff: dateTime.getMilliseconds(),
z: this.zoneInformation(dateTime, 'z'), //-4
zz: this.zoneInformation(dateTime, 'zz'), //-04
zzz: this.zoneInformation(dateTime, 'zzz') //-0400
// z: this.zoneInformation(dateTime, 'z'), //-4
// zz: this.zoneInformation(dateTime, 'zz'), //-04
// zzz: this.zoneInformation(dateTime, 'zzz') //-0400
};

return format.replace(this.REGEX_FORMAT, (match, $1) => {
Expand Down
Loading

0 comments on commit 5043731

Please sign in to comment.