From: Viktor Date: Thu, 10 Mar 2022 20:31:54 +0000 (+0100) Subject: Parse dates when entered without separators X-Git-Tag: beta-1.6.1~118^2 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F250%2Fhead;p=thirdparty%2Fpaperless-ngx.git Parse dates when entered without separators This adds date dividers if none are entered. It also adds the current year if it wasn't entered. This allows users to just enter 1003, 100322, 10032022 and have it expanded to 10.03.2022, in the case of the German format. (All other formats are also supported) It also replaces commas with the date divider. This allows quick entry of the date on the numpad. --- diff --git a/src-ui/src/app/utils/ngb-date-parser-formatter.ts b/src-ui/src/app/utils/ngb-date-parser-formatter.ts index 07d31a5252..ceb773f9fd 100644 --- a/src-ui/src/app/utils/ngb-date-parser-formatter.ts +++ b/src-ui/src/app/utils/ngb-date-parser-formatter.ts @@ -27,7 +27,67 @@ export class LocalizedDateParserFormatter extends NgbDateParserFormatter { ) } + /** + * This adds date separators if none are entered. + * It also adds the current year if it wasn't entered. + * + * This allows users to just enter 1003, 100322, 10032022 and + * have it expanded to 10.03.2022, in the case of the German format. + * (All other formats are also supported) + * + * It also replaces commas with the date separator. + * This allows quick entry of the date on the numpad. + */ + private preformatDateInput(value: string): string { + let inputFormat = this.getDateInputFormat() + let dateSeparator = inputFormat.replace(/[dmy]/gi, '').charAt(0) + + value = value.replace(/,/g, dateSeparator) + + if (value.includes(dateSeparator)) { return value } + + if (value.length == 4 && inputFormat.substring(0, 4) != 'yyyy') { + return value.substring(0, 2) + + dateSeparator + + value.substring(2, 4) + + dateSeparator + + new Date().getFullYear() + } + else if (value.length == 4 && inputFormat.substring(0, 4) == 'yyyy') { + return new Date().getFullYear() + + dateSeparator + + value.substring(0, 2) + + dateSeparator + + value.substring(2, 4) + } + else if (value.length == 6) { + return value.substring(0, 2) + + dateSeparator + + value.substring(2, 4) + + dateSeparator + + value.substring(4, 6) + } + else if (value.length == 8 && inputFormat.substring(0, 4) != 'yyyy') { + return value.substring(0, 2) + + dateSeparator + + value.substring(2, 4) + + dateSeparator + + value.substring(4, 8) + } + else if (value.length == 8 && inputFormat.substring(0, 4) == 'yyyy') { + return value.substring(0, 4) + + dateSeparator + + value.substring(4, 6) + + dateSeparator + + value.substring(6, 8) + } + else { + return value + } + } + parse(value: string): NgbDateStruct | null { + value = this.preformatDateInput(value); let match = this.getDateParseRegex().exec(value) if (match) { let dateStruct = {