Define "doesn't work".
const date = moment("2015-07-02"); // Thursday Feb 2015
const dow = date.day();
console.log(dow);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
This prints "4", as expected.
Answer from c0xc on Stack OverflowDefine "doesn't work".
const date = moment("2015-07-02"); // Thursday Feb 2015
const dow = date.day();
console.log(dow);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
This prints "4", as expected.
If you are specifically looking for the 1-7 approach...
This is the ISO weekday number. moment.js has also taken this into account. Use isoWeekday()
console.log(moment().isoWeekday()); // returns 1-7 where 1 is Monday and 7 is Sunday
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Seeing as I wrote this answer on a Tuesday, today this gives me a 2.
Hello, I am working with version 1.1.16 of Obsidian and trying to move from daily to weekly notes.
How do I print the first and the end date of the week? The Moment JS documentation suggests the following two options
- `moment#endOf('week')` -
- `moment().startOf('week')` - I am certain this won't be possible since this is the JS way.
2. Is there a way to evaluate JS expressions as well?
3. How do I print all the dates and the day incrementally in that week?
EDIT - additionally I want to understand how does the evaluation of such expressions works in Obsidian. Currently I am only able to leverage these expressions when using a template.
You are on the right track. The weekday function can be used to get the Monday and Friday of the current week.
var now = moment();
var monday = now.clone().weekday(1);
var friday = now.clone().weekday(5);
var isNowWeekday = now.isBetween(monday, friday, null, '[]');
console.log(`now: ${now}`);
console.log(`monday: ${monday}`);
console.log(`friday: ${friday}`);
console.log(`is now between monday and friday: ${isNowWeekday}`);
Direct link to the Moment.js docs on the weekday function here: https://momentjs.com/docs/#/get-set/weekday/
Getting first & last day of current week is as simple as the following:
let currentDate = moment();
let weekStart = currentDate.clone().startOf('week');
let weekEnd = currentDate.clone().endOf('week');
Then you can format it
let formatted = weekEnd.format('YYYY-MM-DD');
btw, I recommend Luxon over moment for many reasons, one of the biggest one is immutability