Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve date calculations #146

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ The syntax for date calculations is best understood through example. Here's how
{{date+5d:YYYY-MM-DD}}
```

You can add or subtract any number of days (`d`), weeks (`w`), months (`m`), or years (`y`).
You can add or subtract any number of days (`d`), weeks (`w`), months (`M`), quarters (`Q`), or years (`y`).

You can also use multiple computations together such as:
```
{{date+1M+1w:YYYY-MM-DD}}
```

This feature is meant for simple use cases, like linking consequtive daily notes. If you need anything more complicated in your template, I highly recommend using the [Templater](https://github.com/SilentVoid13/Templater) plugin in conjunction with Periodic Notes.

Expand Down
39 changes: 27 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export function isMetaPressed(e: MouseEvent | KeyboardEvent): boolean {
return Platform.isMacOS ? e.metaKey : e.ctrlKey;
}

let regexpCalc = /(([+-]\d+)([yqmwdhs]))/gi
let getCalcs = (s) => Array.from(s.matchAll(regexpCalc), match => ({
calc: match[1],
delta: match[2],
unit: match[3]
}))

function getDaysOfWeek(): string[] {
const { moment } = window;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -56,16 +63,18 @@ export function applyTemplateTransformations(
.replace(/{{\s*yesterday\s*}}/gi, date.clone().subtract(1, "day").format(format))
.replace(/{{\s*tomorrow\s*}}/gi, date.clone().add(1, "d").format(format))
.replace(
/{{\s*(date|time)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
(_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
/{{\s*(date|time)\s*(([+-]\d+)([yqmwdhs]))*\s*(:.+?)?}}/gi,
(match, _timeOrDate, calc, _timeDelta, _unit, momentFormat) => {
const now = window.moment();
const currentDate = date.clone().set({
hour: now.get("hour"),
minute: now.get("minute"),
second: now.get("second"),
});
if (calc) {
currentDate.add(parseInt(timeDelta, 10), unit);
getCalcs(match).forEach(item => {
currentDate.add(parseInt(item.delta, 10), item.unit)
});
}

if (momentFormat) {
Expand All @@ -88,8 +97,8 @@ export function applyTemplateTransformations(

if (granularity === "month") {
templateContents = templateContents.replace(
/{{\s*(month)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
(_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
/{{\s*(month)\s*(([+-]\d+)([yqmwdhs]))*\s*(:.+?)?}}/gi,
(match, _timeOrDate, calc, _timeDelta, _unit, momentFormat) => {
const now = window.moment();
const monthStart = date
.clone()
Expand All @@ -100,7 +109,9 @@ export function applyTemplateTransformations(
second: now.get("second"),
});
if (calc) {
monthStart.add(parseInt(timeDelta, 10), unit);
getCalcs(match).forEach(item => {
monthStart.add(parseInt(item.delta, 10), item.unit)
});
}

if (momentFormat) {
Expand All @@ -113,8 +124,8 @@ export function applyTemplateTransformations(

if (granularity === "quarter") {
templateContents = templateContents.replace(
/{{\s*(quarter)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
(_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
/{{\s*(quarter)\s*(([+-]\d+)([yqmwdhs]))*\s*(:.+?)?}}/gi,
(match, _timeOrDate, calc, _timeDelta, _unit, momentFormat) => {
const now = window.moment();
const monthStart = date
.clone()
Expand All @@ -125,7 +136,9 @@ export function applyTemplateTransformations(
second: now.get("second"),
});
if (calc) {
monthStart.add(parseInt(timeDelta, 10), unit);
getCalcs(match).forEach(item => {
monthStart.add(parseInt(item.delta, 10), item.unit)
});
}

if (momentFormat) {
Expand All @@ -138,8 +151,8 @@ export function applyTemplateTransformations(

if (granularity === "year") {
templateContents = templateContents.replace(
/{{\s*(year)\s*(([+-]\d+)([yqmwdhs]))?\s*(:.+?)?}}/gi,
(_, _timeOrDate, calc, timeDelta, unit, momentFormat) => {
/{{\s*(year)\s*(([+-]\d+)([yqmwdhs]))*\s*(:.+?)?}}/gi,
(match, _timeOrDate, calc, _timeDelta, _unit, momentFormat) => {
const now = window.moment();
const monthStart = date
.clone()
Expand All @@ -150,7 +163,9 @@ export function applyTemplateTransformations(
second: now.get("second"),
});
if (calc) {
monthStart.add(parseInt(timeDelta, 10), unit);
getCalcs(match).forEach(item => {
monthStart.add(parseInt(item.delta, 10), item.unit)
});
}

if (momentFormat) {
Expand Down