mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Merge pull request #250 from GruberViktor/date_parser
Parse dates when entered without separators
This commit is contained in:
commit
73a597855f
@ -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 = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user