feat: allow to write date via Quick Add Magic in DMY format with dots (#744)

Co-authored-by: Hudint Finn Weigand <dev@hudint.de>
This commit is contained in:
Finn Weigand 2025-05-09 15:08:17 +02:00 committed by GitHub
parent b485abfb65
commit fbec58a50b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 35 deletions

View File

@ -73,7 +73,9 @@
<li>End of month</li>
<li>In 5 days [hours/weeks/months]</li>
<li>Tuesday ({{ $t('task.quickAddMagic.dateWeekday') }})</li>
<li>17/02/2021</li>
<li>02/17/2021</li>
<li>2021-02-17</li>
<li>17.02.2021</li>
<li>Feb 17 ({{ $t('task.quickAddMagic.dateCurrentYear') }})</li>
<li>17th ({{ $t('task.quickAddMagic.dateNth', {day: '17'}) }})</li>
</ul>

View File

@ -85,11 +85,11 @@ export const parseDate = (text: string, now: Date = new Date()): dateParseResult
if (parsed.date === null) {
const time = addTimeToDate(text, new Date(now), parsed.foundText)
if (time.date !== null && +now !== +time.date) {
return time
}
return {
newText: replaceAll(text, parsed.foundText, ''),
date: parsed.date,
@ -138,46 +138,50 @@ const addTimeToDate = (text: string, date: Date, previousMatch: string | null):
}
export const getDateFromText = (text: string, now: Date = new Date()) => {
const fullDateRegex = /(^| )([0-9][0-9]?\/[0-9][0-9]?\/[0-9][0-9]([0-9][0-9])?|[0-9][0-9][0-9][0-9]\/[0-9][0-9]?\/[0-9][0-9]?|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?)/ig
const dateRegexes: RegExp[] = [
/(^| )(?<found>(?<month>[0-9][0-9]?)\/(?<day>[0-9][0-9]?)(\/(?<year>[0-9][0-9]([0-9][0-9])?))?)($| )/gi,
/(^| )(?<found>(?<year>[0-9][0-9][0-9][0-9]?)\/(?<month>[0-9][0-9]?)\/(?<day>[0-9][0-9]))($| )/gi,
/(^| )(?<found>(?<year>[0-9][0-9][0-9][0-9]?)-(?<month>[0-9][0-9]?)-(?<day>[0-9][0-9]))($| )/gi,
/(^| )(?<found>(?<day>[0-9][0-9]?)\.(?<month>[0-9][0-9]?)(\.(?<year>[0-9][0-9]([0-9][0-9])?))?)($| )/gi,
]
// 1. Try parsing the text as a "usual" date, like 2021-06-24 or 06/24/2021
let results: string[] | null = fullDateRegex.exec(text)
let result: string | null = results === null ? null : results[0]
let foundText: string | null = result
let result: string | null = null
let results: RegExpExecArray | null = null
let foundText: string | null = ''
let containsYear = true
// 1. Try parsing the text as a "usual" date, like 2021-06-24 or "06/24/2021" or "27/01" or "01/27"
for (const dateRegex of dateRegexes) {
results = dateRegex.exec(text)
if (results !== null) {
const {day, month, year, found} = {...results.groups}
let tmp_year = year
if (tmp_year === undefined) {
tmp_year = year ?? now.getFullYear()
containsYear = false
}
result = `${month}/${day}/${tmp_year}`
result = !isNaN(new Date(result).getTime()) ? result : `${day}/${month}/${tmp_year}`
result = !isNaN(new Date(result).getTime()) ? result : null
if(result !== null){
foundText = found
break
}
}
}
// 2. Try parsing the date as something like "jan 21" or "21 jan"
if (result === null) {
// 2. Try parsing the date as something like "jan 21" or "21 jan"
const monthRegex = new RegExp(`(^| )(${monthsRegexGroup} [0-9][0-9]?|[0-9][0-9]? ${monthsRegexGroup})`, 'ig')
results = monthRegex.exec(text)
result = results === null ? null : `${results[0]} ${now.getFullYear()}`.trim()
foundText = results === null ? '' : results[0].trim()
containsYear = false
if (result === null) {
// 3. Try parsing the date as "27/01" or "01/27"
const monthNumericRegex = /(^| )([0-9][0-9]?\/[0-9][0-9]?)/ig
results = monthNumericRegex.exec(text)
// Put the year before or after the date, depending on what works
result = results === null ? null : `${now.getFullYear()}/${results[0]}`
if (result === null) {
return {
foundText,
date: null,
}
}
foundText = results === null ? '' : results[0]
if (result === null || isNaN(new Date(result).getTime())) {
result = results === null ? null : `${results[0]}/${now.getFullYear()}`
}
if (result === null || (isNaN(new Date(result).getTime()) && foundText !== '')) {
const parts = foundText.split('/')
result = `${parts[1]}/${parts[0]}/${now.getFullYear()}`
}
}
}
if (result === null) {
return {
foundText,

View File

@ -515,7 +515,10 @@ describe('Parse Task Text', () => {
'dec 21': '2021-12-21',
'Dec 21': '2021-12-21',
'december 21': '2021-12-21',
'December 21': '2021-12-21',
'01.02.2021': '2021-2-1',
'01.02': '2022-2-1',
'01.10': '2021-10-1',
'01.02.25': '2025-2-1',
} as Record<string, string | null>
for (const c in cases) {