Weekly note title based on file title

What I’m trying to do

I would like to generate dates in my weekly notes based on the filename which is handled by Periodic Notes such that I can create weekly notes in advance or retrosperspectively through the calendar plug-in.
I do the same in my daily note with the following command:

# <% moment(tp.file.title,'YYYY-MM-DD').format("[Week ]WW - MMMM YYYY") %>

My weekly notes have their filename defined by:

[W]-YYYY-MM-WW

For example, “W-2024-02-07” for week 7 of 2024(which is in Feb).

Things I have tried

I have been trying heaps of different commands, but none works.

# <% moment(tp.file.title, '[W]-YYYY-MM-WW').format('[Week ]WW - MMMM YYYY') %>

gives me # Week 05 - February 2024 for a file called W-2024-02-08.

I also tried the date syntax:

# <%tp.date.now("[Week ]WW - MMMM YYYY", 0, tp.file.title, "[W]-YYYY-MM-WW")%>

but this also results in # Week 05 - February 2024.

If I use

<% tp.date.now("[Week ]WW - MMMM YYYY", 0) %>

this correctly gives me Week 08 - February 2024, however, it only works if I am creating the node during the current week.

Any help is greatly appreciated!

1 Like

Moment isn’t parsing your input date format correctly.
It works through the tokens from left to right, so for W-2024-02-07 and matching string [W]-YYYY-MM-WW:

It builds the date object from

  1. 2024 year
  2. 02 month
  3. 07 week

In Step 2, moment assigns the date to be the first of the month.
Then, for some reason, it doesn’t like the combination of finding the day from the week token in Step 3 after the month has been parsed. There may be a reason for this or a bug at the moment.

Resolution

Just don’t parse the month in your string [W]-YYYY-[MM]-WW.

 moment('W-2024-02-07', '[W]-YYYY-[MM]-WW').format('[Week ]WW - MMMM YYYY') 
 // returns: 'Week 07 - February 2024'
1 Like

The solution by @evank is correct, thanks a lot!
If anyone would like to have the header of a weekly note including navigation to previous and next week:

---
<%*
const fileFormat = "[W]-YYYY-[MM]-WW"
const linkFormat = "[W]-YYYY-MM-WW"
const displayFormat = "YYYY-MM-[W]WW"
const titleFormat = "[Week ]WW - MMMM YYYY"
%>
week: <% tp.date.now(displayFormat, 0, tp.file.title, fileFormat) %>
---
#  <% tp.date.now(titleFormat, 0, tp.file.title, fileFormat) %>

<< [[<% tp.date.now(linkFormat, -7, tp.file.title, fileFormat) %>]] | [[<% tp.date.now(linkFormat, 7, tp.file.title, fileFormat) %>]]>>

Which will give you something like this:

Do note that using a format like you do here, will also make dataview confused since your example title will be interpreted by default as february 7th, 2024 in file.day

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.