Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  25] [ 1]  / answers: 1 / hits: 38074  / 11 Years ago, mon, september 30, 2013, 12:00:00

I seem to have a bit of a problem getting the previous Monday given a particular date. I'm trying to use Moment js for the task. Obviously, I can do it by hand, but found it curious that I couldn't get it to work using the example in the moment.js documentation on their website: http://momentjs.com/docs/#/get-set/day/.



I was trying something like:



moment([2013, 08, 15, 15, 20]).day(-1).format('ddd, MMM DD')


which results in the 'two days ago' date, that being September 13 instead of the expected September 9th.



Does anybody have a clue here? Thanks.


More From » momentjs

 Answers
49

Here is how it works:



moment().day(1) // this monday
moment().day(-6) // last monday, think of it as this monday - 7 days = 1 - 7 = -6


Same applies in other direction:



moment().day(8) // next monday, or this monday + 7 days = 1 + 7 = 8


Your code moment().day(-1) can be explained as this Sunday - 1 day = 0 - 1 = -1
or this Saturday - 7 days = 6 - 7 = -1


[#75326] Sunday, September 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karyme

Total Points: 545
Total Questions: 102
Total Answers: 120

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
;