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

159
Visualizações
Display year to date

I am trying to display data for a "Year to Date" range. I need it to show all the dates ranging from the first day of January 2021 until the current date (whatever day today is).

I previously had the data showing only the previous 30 days and had:

const today = new Date();
    const startDate =
      this.startDate || new Date(today.getFullYear(), today.getMonth(), today.getDate() - 30);
    const endDate = this.endDate || today;

How can I get the data to show from January 1st, 2021 to whatever the current day is?

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

0

As others have answered, just "better" — IMHO of course. :-)

// Default end is current date
function getDateRange(startDate, endDate = new Date()) {
  // Default start to 1 Jan of endDate year
  if (!startDate) {
    startDate = new Date(endDate.getFullYear(), 0);
  // Otherwise copy startDate so don't affect original
  } else {
    startDate = new Date(+startDate);
  }
  let result = [];
  // Push timestamps into an array until endDate
  while (startDate <= endDate) {
    // Push date in YYYY-MM-DD format into result array
    result.push(startDate.toLocaleDateString('en-CA'));
    // Increment startDate
    startDate.setDate(startDate.getDate() + 1);
  }
  
  return result;
}

// Dates from 1 Jan to today
console.log(getDateRange());

about 4 years ago · Juan Pablo Isaza Relatório

0

This will work for every current year

const aDay = 24*60*60*1000;
const end = new Date()
// normalise - otherwise I would see last day of previous year until yesterday due to timezones
end.setHours(15,0,0,0); 
const start = new Date(end.getFullYear(),0,0,15,0,0,0); // 31/12/previous year to get date 0
const days = (end.getTime()-start.getTime())/aDay
let t = start.getTime();
const dates =  [...Array(days)] // create an array of days 
  .map(() => new Date(t+=aDay).toISOString().split('T')[0])

console.log(dates)

Using while

const aDay = 24*60*60*1000;
const end = new Date()
// normalise - otherwise I would see last day of previous year until yesterday due to timezones
end.setHours(15,0,0,0); 
const start = new Date(end.getFullYear(),0,0,15,0,0,0); // 31/12/previous year to get date 0
const days = (end.getTime()-start.getTime())/aDay
let t = start.getTime();
const endTime = end.getTime();
const dates = [];
while (t<endTime) {
  dates.push(new Date(t+=aDay).toISOString().split('T')[0])
}

console.log(dates)

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is how to create an array dateArray of all dates between the first of this year and today:

Date.prototype.addDays = function(days) {
  var date = new Date(this.valueOf());
  date.setDate(date.getDate() + days);
  return date;
}

const today = new Date()
const startDate = new Date(today.getFullYear(), 0, 1);


var dateArray = new Array();
var currentDate = startDate;
while (currentDate <= today) {
  dateArray.push(new Date(currentDate));
  currentDate = currentDate.addDays(1);
}


console.log(dateArray)

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