I have a specific requirement to display a subscription date range on a webpage in the following format:
31 May 2023 — 30 June 2023
When a user subscribes, the backend sends a fixed subscription start date that remains constant. For example, if a user subscribes on 31 May 2023, the start date will be:
const startDate = "2023-05-31T10:16:14+00:00"
In addition, there are nextDates
when new credits will be added to the user's account.
const nextDate = "2023-06-30T10:16:20.404358+00:00",
const nextDate1 = "2023-07-31T10:16:20.404358+00:00",
const nextDate2 = "2023-08-31T10:16:20.404358+00:00",
My query is how can I determine the start date for each nextDate
?
The desired output should be as follows:
const startDate = "2023-05-31T10:16:14+00:00"
const nextDate = "2023-06-30T10:16:20.404358+00:00" // The start date should be 31 May 2023
const nextDate1 = "2023-07-31T10:16:20.404358+00:00" // The start date should be 30 June 2023
I attempted a solution but ended up with incorrect results:
const getPreviousInterval = (nextDate, months) => {
const date = new Date(nextDate);
const dateMonthsAgoUnix = date.setMonth(date.getMonth() - months);
return new Date(dateMonthsAgoUnix);
};
getPreviousInterval("2023-06-30T10:16:20.404358+00:00", 1);
// Incorrect output is 2023-05-30T10:16:20.404Z
// Correct output should be 2023-05-31T10:16:14+00:00