filter array of timestamps by 'last week', 'last month' and 'last year'

Viewed 595

I have an array of timestamps and values where I'm converting each timestamp to a date. I have some function where it shows the date from oldest to newest. I want to calculate last week, last month and last year based on today's date.

const myData = [
  {timestamp: 1382086394000, value: 200},
  {timestamp: 1382086394001, value: 200},
  {timestamp: 1232131231232, value: 300},
  {timestamp: 2131231241212, value: 400},
  {timestamp: 1234124124112, value: 285},
  {timestamp: 1251251251251, value: 123},
  {timestamp: 1241241241241, value: 512},
  {timestamp: 1241241241241, value: 124},
  {timestamp: 2312312123121, value: 600},
];

This is the array looks like, keep in mind that I already have converted timestamp to an acutal date.

2 Answers

You can use Moment.js manipulating functions like add or subtract for this.

let timestamp = 1382086394000; 
console.log(moment(timestamp )) // Moment<2013-10-18T08:53:14+00:00>

// this is date after a week from the given timestamp
console.log(moment(timestamp ).add(1, 'w')) // Moment<2013-10-25T08:53:14+00:00>

You can do this for getting the last week, month and year by subtracting accordingly. Moment.js takes care of all the edge cases you might encounter.

You can easily calculate the last year (assuming full year Jan 1st to Dec 31st 2020 if current year is 2021) and last month (assuming full month Jan 1st to Jan 31st if current date is Feb) and last week (as for your comment the last 7 days, including today) as follows.

I use an semi-open interval (ie the end of the period is not included) so you don't have to create timestamps like 23:59:59.999. Thus you have to compare with < for the end.

Using getMonth()-1 or getDate()-6 will make use of the internal calcuations in the constructor of the date object. So for instance new Date(2021, 0, -3) will create the 28th of December 2020

let today = new Date();
console.log(today.toLocaleString());

let lastYearStart = new Date(today.getFullYear()-1, 0, 1);
let lastYearEnd = new Date(today.getFullYear(), 0, 1);

console.log(lastYearStart.toLocaleString());
console.log(lastYearEnd.toLocaleString());


let lastMonthEnd = new Date(today.getFullYear(), today.getMonth(), 1);
let lastMonthStart = new Date(lastMonthEnd.getFullYear(), lastMonthEnd.getMonth()-1, 1);

console.log(lastMonthStart.toLocaleString());
console.log(lastMonthEnd.toLocaleString());

let lastWeekStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()-6);
let lastWeekEnd = new Date(today.getFullYear(), today.getMonth(), today.getDate()+1);

console.log(lastWeekStart.toLocaleString());
console.log(lastWeekEnd.toLocaleString());

const mydata = [
  {timestamp: 1382086394000, value: 200},
  {timestamp: 1382086394001, value: 200},
  {timestamp: 1232131231232, value: 300},
  {timestamp: 2131231241212, value: 400},
  {timestamp: 1234124124112, value: 285},
  {timestamp: 1251251251251, value: 123},
  {timestamp: 1241241241241, value: 512},
  {timestamp: 1241241241241, value: 124},
  {timestamp: 2312312123121, value: 600},
];

for (let d of mydata) {
  if (d.timestamp >= lastWeekStart.getTime() && d.timestamp < lastWeekEnd.getTime())
    console.log("last week");
  else if (d.timestamp >= lastMonthStart.getTime() && d.timestamp < lastMonthEnd.getTime())
    console.log("last month");
  else if (d.timestamp >= lastYearStart.getTime() && d.timestamp < lastYearEnd.getTime())
    console.log("last year");
}

Of course, a certain timestamp may (depending on the current date) be all three categories, last week, last month, last year. You may need to adapt classification accordingly (ie use only if instead of else if) if it should be able to classified one timestamp in more than one category

Related