mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-09-16 21:55:37 -05:00
@@ -1,6 +1,9 @@
|
||||
import { Injectable } from "@angular/core"
|
||||
import { NgbDateParserFormatter, NgbDateStruct } from "@ng-bootstrap/ng-bootstrap"
|
||||
import { SettingsService } from "../services/settings.service"
|
||||
import { Injectable } from '@angular/core'
|
||||
import {
|
||||
NgbDateParserFormatter,
|
||||
NgbDateStruct,
|
||||
} from '@ng-bootstrap/ng-bootstrap'
|
||||
import { SettingsService } from '../services/settings.service'
|
||||
|
||||
@Injectable()
|
||||
export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
|
||||
@@ -20,45 +23,71 @@ export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
|
||||
*/
|
||||
private getDateParseRegex() {
|
||||
return new RegExp(
|
||||
"^" + this.getDateInputFormat()
|
||||
.replace('dd', '(?<day>[0-9]+)')
|
||||
.replace('mm', '(?<month>[0-9]+)')
|
||||
.replace('yyyy', '(?<year>[0-9]+)')
|
||||
.split('.').join('\\.\\s*') + "$" // allow whitespace(s) after dot (specific for German)
|
||||
)
|
||||
'^' +
|
||||
this.getDateInputFormat()
|
||||
.replace('dd', '(?<day>[0-9]+)')
|
||||
.replace('mm', '(?<month>[0-9]+)')
|
||||
.replace('yyyy', '(?<year>[0-9]+)')
|
||||
.split('.')
|
||||
.join('\\.\\s*') +
|
||||
'$' // allow whitespace(s) after dot (specific for German)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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 strips any separators before running formatting and pads
|
||||
* any parts of the string, e.g. allowing for 1/2/22,
|
||||
* which allows quick entry of the date on the numpad.
|
||||
* which allows quick entry of the date on the numpad.
|
||||
*/
|
||||
private preformatDateInput(value: string): string {
|
||||
const inputFormat = this.getDateInputFormat()
|
||||
const dateSeparator = inputFormat.replace(/[dmy]/gi, '').charAt(0)
|
||||
|
||||
|
||||
if (this.separatorRegExp.test(value)) {
|
||||
// split on separator, pad & re-join without separator
|
||||
value = value.split(this.separatorRegExp).map(segment => segment.padStart(2,'0')).join('')
|
||||
}
|
||||
value = value
|
||||
.split(this.separatorRegExp)
|
||||
.map((segment) => segment.padStart(2, '0'))
|
||||
.join('')
|
||||
}
|
||||
|
||||
if (value.length == 4 && inputFormat.substring(0, 4) != 'yyyy') {
|
||||
return [value.substring(0, 2), value.substring(2, 4), new Date().getFullYear()].join(dateSeparator)
|
||||
return [
|
||||
value.substring(0, 2),
|
||||
value.substring(2, 4),
|
||||
new Date().getFullYear(),
|
||||
].join(dateSeparator)
|
||||
} else if (value.length == 4 && inputFormat.substring(0, 4) == 'yyyy') {
|
||||
return [new Date().getFullYear(), value.substring(0, 2), value.substring(2, 4)].join(dateSeparator)
|
||||
return [
|
||||
new Date().getFullYear(),
|
||||
value.substring(0, 2),
|
||||
value.substring(2, 4),
|
||||
].join(dateSeparator)
|
||||
} else if (value.length == 6) {
|
||||
return [value.substring(0, 2), value.substring(2, 4), value.substring(4, 6)].join(dateSeparator)
|
||||
return [
|
||||
value.substring(0, 2),
|
||||
value.substring(2, 4),
|
||||
value.substring(4, 6),
|
||||
].join(dateSeparator)
|
||||
} else if (value.length == 8 && inputFormat.substring(0, 4) != 'yyyy') {
|
||||
return [value.substring(0, 2), value.substring(2, 4), value.substring(4, 8)].join(dateSeparator)
|
||||
return [
|
||||
value.substring(0, 2),
|
||||
value.substring(2, 4),
|
||||
value.substring(4, 8),
|
||||
].join(dateSeparator)
|
||||
} else if (value.length == 8 && inputFormat.substring(0, 4) == 'yyyy') {
|
||||
return [value.substring(0, 4), value.substring(4, 6), value.substring(6, 8)].join(dateSeparator)
|
||||
return [
|
||||
value.substring(0, 4),
|
||||
value.substring(4, 6),
|
||||
value.substring(6, 8),
|
||||
].join(dateSeparator)
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
@@ -71,9 +100,9 @@ export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
|
||||
let dateStruct = {
|
||||
day: +match.groups.day,
|
||||
month: +match.groups.month,
|
||||
year: +match.groups.year
|
||||
year: +match.groups.year,
|
||||
}
|
||||
if (dateStruct.year <= (new Date().getFullYear() - 2000)) {
|
||||
if (dateStruct.year <= new Date().getFullYear() - 2000) {
|
||||
dateStruct.year += 2000
|
||||
} else if (dateStruct.year < 100) {
|
||||
dateStruct.year += 1900
|
||||
@@ -87,9 +116,9 @@ export class LocalizedDateParserFormatter extends NgbDateParserFormatter {
|
||||
format(date: NgbDateStruct | null): string {
|
||||
if (date) {
|
||||
return this.getDateInputFormat()
|
||||
.replace('dd', date.day.toString().padStart(2, '0'))
|
||||
.replace('mm', date.month.toString().padStart(2, '0'))
|
||||
.replace('yyyy', date.year.toString().padStart(4, '0'))
|
||||
.replace('dd', date.day.toString().padStart(2, '0'))
|
||||
.replace('mm', date.month.toString().padStart(2, '0'))
|
||||
.replace('yyyy', date.year.toString().padStart(4, '0'))
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
|
Reference in New Issue
Block a user