]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Parse dates when entered without separators 250/head
authorViktor <viktor@fermentationculture.eu>
Thu, 10 Mar 2022 20:31:54 +0000 (21:31 +0100)
committerViktor <viktor@fermentationculture.eu>
Thu, 10 Mar 2022 20:31:54 +0000 (21:31 +0100)
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.

src-ui/src/app/utils/ngb-date-parser-formatter.ts

index 07d31a5252146c4335b6f91e6b1325400e575da7..ceb773f9fda6ce4eec9ce980c0100807cc90d0e2 100644 (file)
@@ -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 = {