Home > other >  Getting last five recent days of a date
Getting last five recent days of a date

Time:01-03

I am trying to get the last recent five days of December 20th of 2022, also trying to convert UTC timezone to Pacific timezone (California timezone).

I tried this solution but it gives the last recent five days of Jan 20th 2023

My solution

const recentFiveDays = new Array(5).fill().map((_, index) => {
  return new Date(new Date().setDate(new Date("2022-12-20").getDate() - index))
    .toISOString()
});
console.log(recentFiveDays);

and I get this output.

[
  '2023-01-19T07:13:55.565Z',
  '2023-01-18T07:13:55.565Z',
  '2023-01-17T07:13:55.566Z',
  '2023-01-16T07:13:55.566Z',
  '2023-01-15T07:13:55.566Z'
]

I am looking to get this output with pacific timezone (California timezone).

[
  '2022-12-19T07:13:55.565Z',
  '2022-12-18T07:13:55.565Z',
  '2022-12-17T07:13:55.566Z',
  '2022-12-16T07:13:55.566Z',
  '2022-12-15T07:13:55.566Z'
]

CodePudding user response:

Let's split it to parts:

new Date("2022-12-20").getDate() gives to you day of month it is 20.

You subtracting n days but in operation new Date().setDate( ... ) you setting day of month for current date. So this code was working only in last month but will not work in future or past.

Answer that will change only few characters in your original code is

recentFiveDays = new Array(5).fill().map((_, index) => {
  return new Date(new Date("2022-12-20").setDate(new Date("2022-12-20").getDate() - index))
    .toISOString()
});
console.log(recentFiveDays);

i will print

"2022-12-20T00:00:00.000Z"
"2022-12-19T00:00:00.000Z"
"2022-12-18T00:00:00.000Z"
"2022-12-17T00:00:00.000Z"
"2022-12-16T00:00:00.000Z"

but I think you can invest time to learn dayjs library that is very lightweight and allow to write the same code in much more cleaner way.


With dayjs you can handle timezone problem using this code

import dayjs from "dayjs";
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc)
dayjs.extend(timezone);

const tz = "America/Los_Angeles"

const recentFiveDays = new Array(5).fill(0).map((_, index) => {
    return dayjs("2022-12-20").tz(tz).subtract(index, 'days').format()
});
console.log(recentFiveDays);

and you will see

[
  '2022-12-19T15:00:00-08:00',
  '2022-12-18T15:00:00-08:00',
  '2022-12-17T15:00:00-08:00',
  '2022-12-16T15:00:00-08:00',
  '2022-12-15T15:00:00-08:00'
]

there is no toIsoString because you can't have timezone in ISO String by definition:

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, as denoted by the suffix Z.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

CodePudding user response:

The only issue with your code is that you didnt used the date string inside the highlighted part. This is the final date object where your date is being decided

new Date(
  new Date("2022-12-20") // Here
    .setDate(new Date("2022-12-20").getDate() - index))

What you did ultimately is you took the date string as "2022-12-20", decremented the date with index from that date and set it to current date. Instead of current date you should set it to new Date("2022-12-20")

Corrected the logic

Start with date "2022-12-20" Get the date from the date object, decrement it with index, create a new date and convert it to ISO String

const recentFiveDays = new Array(5).fill().map((_, index) =>  new Date(new Date("2022-12-20").setDate(new Date("2022-12-20").getDate() - index)));
console.log(recentFiveDays);

  • Related