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

172
Visualizações
How to get month name for a given year and week in Javascript

I have 2 form fields in HTML namely Year and Week.

Once the user selects a Year and a week from the dropdowns, I want to display the month Name for the selected year and week.

Can anyone help me to get month name based on year and iso week numbers.

For example: If I selected year as 2022 and week no as 16, then the expected output is April as a month name.

This shoud be a function that returns the month name for a given year and week.

Below is my sampke code.

getMonthName() {
    const year  =  2022;
    const week  = 16;
    console.log(month);
  }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Example: https://codesandbox.io/s/damp-wood-nynj7f?file=/src/app/app.component.ts

  1. Create mapper for your months:
  2. 4 weeks = 1 month, then get input value of week covert to number and divide 4. (Example: 16 / 4 === 4)
  3. The result is a key of your mapper: months[divided result]

The best is a using Date libraries such as: dayjs, date-fns, moment, luxon or other.

const months = {
    1: "January",
    2: "February"
}

changeWeekHandler(e) {
   const week = parseInt(e.target.value)
   const month = Math.ceil(week / 4) // for round number to the upper (example: Math.ceil(0.25) -> 1)
   this[your input field] = months[month]
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Really interesting form. I came up with a solution like this;

I do not suggest you divide by 4 weeks per month because not all months have 4 weeks. But from your input, you always can know the day of year from a number of weeks

numberOfWeek * 7 = numberOfDay

create a method :

const dateFromDay = (year, day = 1) => {
  const date = new Date(year, 0);
  return new Date(date.setDate(day));
}

// call the method with your inputs

const myDate = edateFromDay(2022,16*7); 
// here you will have the full date 

// if you want just the month then 
myDate.getMonth();

For exact your need :

//Declare months 
  const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
// Declare method for receiving your month name
const dateFromWeek = (year, week = 1) => {
  
  const date = new Date(year, 0);
  return monthNames[new Date(date.setDate(week*7)).getMonth()];
}
//When you use 
dateFromWekk(2020,3); //just write year and week, you will get the name of month
about 4 years ago · Juan Pablo Isaza Relatório

0

The following will display the month name (short form) for the given ISO week number and year.

function getMonthName(week, year) {
let d =new Date(year,0,1+(week-1)*7);
d.getUTCDay()<5?d.setUTCDate(d.getUTCDate()-d.getUTCDay()+1):d.setUTCDate(d.getUTCDate()+8-d.getUTCDay());
return (""+d).split(" ")[1];
}

console.log(getMonthName(1,2022));
console.log(getMonthName(11,2022));
console.log(getMonthName(16,2022));

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