Additional Date Functions

Here is some space-script I have created for some additional date functions that I use…

silverbullet.registerFunction({name: "weekNumberFor"}, (date) => {
  const dateObj = Temporal.PlainDate.from(date);
  return dateObj.weekOfYear;
})
silverbullet.registerFunction({name: "monthNumberFor"}, (date) => {
  const dateObj = Temporal.PlainDate.from(date);
  return dateObj.month;
})

silverbullet.registerFunction({name: "monthNumber"}, () => {
  return Temporal.Now.plainDateISO().month;
})

silverbullet.registerFunction({name: 'weekNumber'},() => {
  return Temporal.Now.plainDateISO().weekOfYear;
})

silverbullet.registerFunction({name: 'weekEnd'}, () => {
  const today = Temporal.Now.plainDateISO();
  const lastDayOfWeek = today.add({days: 7 - today.dayOfWeek});
  return lastDayOfWeek.toString();
})
silverbullet.registerFunction({name: 'year'}, () => {
return Temporal.Now.plainDateISO().year;
})

silverbullet.registerFunction({name: 'journalDate'}, () => {
const date = Temporal.Now.plainDateISO();

// Extract the components
const month = String(date.month).padStart(2, '0'); // Ensures 2 digits
const day = String(date.day).padStart(2, '0'); // Ensures 2 digits
const dayOfWeek = date.dayOfWeek;

// Map the dayOfWeek to its abbreviation
const dayOfWeekMap = {
    1: 'Mon',
    2: 'Tues',
    3: 'Wed',
    4: 'Thurs',
    5: 'Fri',
    6: 'Sat',
    7: 'Sun'
};
const dayOfWeekAbbrev = dayOfWeekMap[dayOfWeek];

// Construct the formatted string
const formattedDate = `${month}-${day}-${dayOfWeekAbbrev}`;
return formattedDate;
})

To view the results you can use this:

```template
**today:** {{today}}
**Week Number for "2024-08-15":** {{weekNumberFor("2024-08-15")}}
**Current Week Number:** {{weekNumber}}
**Week Start -> Week End:** {{weekStart}} -> {{weekEnd}}
**Month Number for "2024-08-15":** {{monthNumberFor("2024-08-15")}}
**Current Month Number:** {{monthNumber}}
**Date formatted for Journal:** {{journalDate}}
```

and it would look like this:

today: 2024-04-16
Week Number for “2024-08-15”: 33
Current Week Number: 16
Week Start → Week End: 2024-04-15 → 2024-04-21
Month Number for “2024-08-15”: 8
Current Month Number: 4
Date formatted for Journal: 04-16-Tues

10 Likes

Adding these here since I use this as well :wink: