Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda