Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!