Manipulação Momentjs

Asked

Viewed 114 times

3

I’m using Moment.js to validate some dates and used the function moment().weeksInYear() to check which week of the year we are in.

I need to make a date forecast, after the user informs me a date, I can calculate how many weeks will pass and we add this to the function of the increment Moment moment().weeksInYear() + semanas.

I need to manipulate the Moment so I can figure out which Friday of the week I calculate.

example:

moment().weeksInYear() // retorna semana 52 
moment().weeksInYear() + semanas // retorna 54

need to use this 54 to know the exact date of Friday of the week 54 of the year.

  • A year does not have 54 weeks, this calculation is wrong. You want to catch the Friday of the second week of the following year. You need to check that the sum of the weeks is greater than the amount of weeks in the year, if it is, add +1 to the current year. :)

1 answer

1


You can get the day of the week by name, in the case: Friday.

// obtendo o número da semana atual (no seu caso, é a soma das semanas).
var week = moment().week();

// obtendo a sexta-feira daquela semana e convertendo em data.
var friday = moment().day('friday').week(week).toDate().toString();

console.log(friday);
<script src="https://momentjs.com/downloads/moment.min.js"></script>

  • It worked, thank you! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.