I have the related.date: {"isoYear":"2021","isoWeek":"31"}
property with the following value received from the backend. How can I receive first date and last date from isoWeek 31 for the isoYear by using momenjs?
Asked
Active
Viewed 279 times
0

NewTech Lover
- 1,185
- 4
- 20
- 44
3 Answers
0
const date = moment().isoWeek(related.date.isoWeek).year(related.date.year)
const first = moment().weekday(0)
const last = moment().weekday(6)
by default the weekday 0 is sunday, if you live in an area where the first weekday is monday, you have to parse that on your own or use configured moment-with-locales-es6

PRSHL
- 1,359
- 1
- 11
- 30
-
Thanks for replying, I think this approach will be a bit problematic for isoMonth since how last date is going to be found? Some months have 30 days some 31 – NewTech Lover Aug 10 '21 at 15:04
-
In this case you could go to the first day of the following month (`moment().add(1, 'months')`) and subtract one day (`moment().subtract(1, 'days')`). This will give you the last day of the given month. – PRSHL Aug 10 '21 at 15:06
0
You can use startOf('isoWeek')
.
// related.date: {"isoYear":"2021","isoWeek":"31"}
const related = {
date: {
isoYear: '2021',
isoWeek: '31',
},
};
const now = moment();
console.log(now.clone().isoWeek(Number(related.date.isoWeek)).startOf('isoWeek'));
<script src="https://cdn.jsdelivr.net/npm/moment@2.29.1/moment.min.js"></script>

kyun
- 9,710
- 9
- 31
- 66
-
I receive value like this: {"isoYear":"2021","isoWeek":"31"} Not the usual object. I think firstly some conversion is needed – NewTech Lover Aug 10 '21 at 15:08
0
You could use startOf('isoWeek')
and endOf('isoWeek')
after you get isoYear
from object and add isoWeek
weeks:
let related = {"isoYear":"2021","isoWeek":"31"};
console.log(moment(related.isoYear, 'YYYY').add(parseInt(related.isoWeek), 'weeks').startOf('isoWeek').format('DD/MM/YYYY'))
console.log(moment(related.isoYear, 'YYYY').add(parseInt(related.isoWeek), 'weeks').endOf('isoWeek').format('DD/MM/YYYY'))
console.log(moment(related.isoYear, 'YYYY').add(parseInt(related.isoWeek), 'weeks').startOf('month').format('DD/MM/YYYY'))
console.log(moment(related.isoYear, 'YYYY').add(parseInt(related.isoWeek), 'weeks').endOf('month').format('DD/MM/YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
EDIT
Now if you want to get month from month
property you could do:
let related = {"isoYear":"2021","isoWeek":"31", "month": "06"}
console.log(moment(related.isoYear, 'YYYY').add(parseInt(related.month) - 1, 'months').startOf('month').format('DD/MM/YYYY'))
console.log(moment(related.isoYear, 'YYYY').add(parseInt(related.month) - 1, 'months').endOf('month').format('DD/MM/YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Note the -1
on add
function: this because in moment
months number start from 0 (January).

Giovanni Esposito
- 10,696
- 1
- 14
- 30
-
1
-
1@NewTechLover yes, if you want that the first day of week is Monday you have to call `startOf('isoWeek')` and `endOf('isoWeek')`. I updated my answer – Giovanni Esposito Aug 10 '21 at 15:14
-
Last question will this structure work if I use it for isoMonth? – NewTech Lover Aug 10 '21 at 15:17
-
@NewTechLover yes it works, take a look at [this](https://stackoverflow.com/questions/39267623/moment-js-get-first-and-last-day-of-current-month) question (I think `isoMonth` does not exists, `month` exists) – Giovanni Esposito Aug 10 '21 at 15:19
-
-
-
@NewTechLover I can't see your code but on snippet it works. Let me see your code (just the line in which you use `startOf('month')`) and I will try to understand why is not working. Would be very useful if you could reproduce your error in codesandbox. – Giovanni Esposito Aug 10 '21 at 17:10
-
I have a month number which is 06 so I need to get start date and end date like did for the week I am using related.month like this: moment(related.isoYear).add(related.month , 'months').startOf('month'), moment(related.isoYear).add(related.month , 'months').endOf('month'), Also tried your sample but both are not working for month case – NewTech Lover Aug 10 '21 at 17:31
-
@NewTechLover watch better my answer: write `moment(related.isoYear)` is not sufficient for moment because you have to indicate the date format (YYYY). Then I see another error: `related.month` seems to be a string... but moment wants a number. So, please, modify your code in this way: `moment(related.isoYear, 'YYYY').add(parseInt(related.month), 'months').startOf('month')` – Giovanni Esposito Aug 10 '21 at 19:11
-
@NewTechLover I updated my answer with the startOf and endOf for month – Giovanni Esposito Aug 10 '21 at 19:18