How to get the number of the day (1 up to 7) as a number instead of a string

What I’m trying to do

I am trying to retrieve the number of the day in the week from the title of my daily page (YYYY-MM-DD) using this:

<% moment(tp.file.title).format("d")

The result is that a Sunday is returned as zero, where I’d like it to be a 7.
To resolve the numbering, I think something needs to be used like (in pseudo code):

correct number = <% moment(tp.file.title).format(“d”)%> +7 - <% moment(tp.file.title).format(“d”)%>

So that Monday is 1, up to Sunday is 7.

Things I have tried

<% moment(tp.file.title).format("d")+7-moment(tp.file.title).format("d")%> 

Which created 16 for monday, 25 for tuesday, 34 for wednesday, 43 for thursday, 51 for friday, 61 for saturday, 7 for sunday :confounded:

You need to convert the textual weekday into a number, and then do the math.

Here is one option for doing this:

<%* wd = parseInt(moment(tp.file.title).format("d")); tR += wd == 0 ? 7 : wd %>

Here we convert it to a number and store it in wd. Then we append to the output using tR += , and the text we append is depending on whether wd == 0 in case we return 7, or else we return the wd itself.

1 Like

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