# Additional Date Functions (v1)

**URL:** https://community.silverbullet.md/t/additional-date-functions-v1/384
**Category:** v1: Troubleshooting (legacy)
**Created:** [April 16, 2024, 4:25am UTC](https://community.silverbullet.md/t/additional-date-functions-v1/384 "2024-04-16T04:25:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![terr-steak](https://community.silverbullet.md/user_avatar/community.silverbullet.md/terr-steak/32/192_2.png) [@terr-steak](https://community.silverbullet.md/u/terr-steak)
#### Post date: [April 16, 2024, 4:25am UTC](https://community.silverbullet.md/t/additional-date-functions-v1/384/1 "2024-04-16T04:25:22Z")

</div>

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

```auto
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

---

<div class="post-metadata">

### Author: ![terr-steak](https://community.silverbullet.md/user_avatar/community.silverbullet.md/terr-steak/32/192_2.png) [@terr-steak](https://community.silverbullet.md/u/terr-steak)
#### Post date: [July 31, 2024, 2:27pm UTC](https://community.silverbullet.md/t/additional-date-functions-v1/384/2 "2024-07-31T14:27:53Z")

</div>

Adding these here since I use this as well 😉

> [@Calculated dates in queries (templates)](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/3):
>
> I wanted to have this to add previous and next buttons to my weekly notes, and came up with this snippet: ```space-script silverbullet.registerFunction("addDays", (date, days) =\> { const plainDate = Temporal.PlainDate.from(date); const offsetDate = plainDate.add(Temporal.Duration.from({ days: Number(days) })); return offsetDate.toString(); }); ``` You can use it like this, [unless you want to pass a negative number](https://github.com/silverbulletmd/silverbullet/issues/691): {{addDays(today, 7)}} Once I have a few more things I think I would pub…

> [@Add "next Sunday" yyyy-MM-DD to your note with a {{next\[Day\]} date function](https://community.silverbullet.md/t/add-next-sunday-yyyy-mm-dd-to-your-note-with-a-next-day-date-function/260):
>
> NextDate Inserter This script adds functions that allow you to insert the yyyy-MM-DD date for the next [day]. e.g. {{nextMonday}} silverbullet.registerFunction({name: "nextSunday"}, () =\> { const currentDayOfWeek = new Date().getDay(); const daysUntilNextSunday = (0 - currentDayOfWeek + 7) % 7; const nextSundayDate = new Date(new Date().getTime() + (daysUntilNextSunday \* 24 \* 60 \* 60 \* 1000)); return `${nextSundayDate.getFullYear()}-${(nextSundayDate.getMonth() + 1).toString().padStart(2, '0…
