From a9aeedd189c3aaec3c07a8b26c79eb12d0d8249c Mon Sep 17 00:00:00 2001 From: jimafisk Date: Fri, 16 Aug 2024 14:01:37 -0400 Subject: [PATCH] Allow N offset days in date widget (#336). --- defaults/core/cms/fields/date.svelte | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/defaults/core/cms/fields/date.svelte b/defaults/core/cms/fields/date.svelte index 0c5ca36f..0ceec488 100644 --- a/defaults/core/cms/fields/date.svelte +++ b/defaults/core/cms/fields/date.svelte @@ -3,9 +3,18 @@ export let schema, parentKeys, field; - let today; + let today, todayPlusN, todayMinusN, N; if (schema && schema[parentKeys]?.options) { today = schema[parentKeys].options.includes("today"); + + const regex = /^today\s?(\+|\-)\s?([0-9]+)$/g; + const match = schema[parentKeys].options.find(option => regex.test(option)); + if (match) { + const [, operation, daysOffset] = new RegExp(regex).exec(match); + todayPlusN = operation === "+"; + todayMinusN = operation === "-"; + N = parseInt(daysOffset); + } } if (today) { @@ -13,6 +22,18 @@ date = makeDate(date); field = formatDate(date, field); } + if (todayPlusN) { + let date = new Date(); + date.setDate(date.getDate() + N); + date = makeDate(date); + field = formatDate(date, field); + } + if (todayMinusN) { + let date = new Date(); + date.setDate(date.getDate() - N); + date = makeDate(date); + field = formatDate(date, field); + } const bindDate = date => { field = formatDate(date, field);