Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
Get the 2nd Friday of a month with MomentJS

I am trying to get the nth Friday of a month with momentjs

where n is in the range [1,4]

So if a user supplies n as 2. Then I need to get the date for the second Friday of the month.

Here is what I tried with no sucess

let startingFrom = new Date("StartDateStringGoesHere");
let n=2;
let nthFriday = moment(startDate).isoWeekday(n); //I can't figure out how to resolve it here

Any ideas will be truly appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Here's a generic function, no library needed

function nthWeekdayOfMonth(year, month, nth, dow) {
    const d = new Date(year, month - 1, 7 * (nth - 1) + 1);
    const w = d.getDay();
    d.setDate(d.getDate() + (7 + dow - w) % 7);
    return d;
}
// second(2) friday(5)
for (let month = 1; month < 13; month++) {
    console.log(nthWeekdayOfMonth(2022, month, 2, 5).toString());
}

// third(3) sunday(0)
for (let month = 1; month < 13; month++) {
    console.log(nthWeekdayOfMonth(2022, month, 3, 0).toString());
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is an example function to do this in m

// Using moment get all Fridays in a month
const searchDate = moment()

function getNthDay(n, day, startDate){
    let daysArray = []
    for (let i = 0; i <= startDate.daysInMonth(); i++) {
        const currentDay = moment().startOf("month").add(i, "days");
        if (currentDay.format('dddd') === day) {
            daysArray.push(currentDay);
        }
    }
    
    return daysArray[n];
}

const nDay = getNthDay(1, 'Friday', searchDate);

console.log(nDay)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda