skip to Main Content

I need to render a subscription date range on a page like this:

31 May 2023 — 30 June 2023

When user subscribes then backend sends a subscription start date (it never changes and always the same). For example if user subscribed on 31 May 2023 then the start date will be:

const startDate = "2023-05-31T10:16:14+00:00"

Then there’re nextDates when a new credits will be added to user 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",

So my question is how can I get the start date for each nextDate?
The output should be like this:

const startDate = "2023-05-31T10:16:14+00:00"
const nextDate = "2023-06-30T10:16:20.404358+00:00" // startDate should be 31 May 2023
const nextDate1 = "2023-07-31T10:16:20.404358+00:00" // startDate should be 30 June 2023

I tried this but the output is incorrect:

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 is 2023-05-31T10:16:14+00:00

4

Answers


  1. const date = new Date(Date.now());
    const oneMonthAgo = new Date(date.setMonth(date.getMonth() + 1));
    
    console.log(oneMonthAgo);
    
    Login or Signup to reply.
  2. // gets today's date
    const date = new Date();
    
    // gets the date from one month ago
    const oneMonthAgo = new Date();
    oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
    
    console.log("Today's date is " + date.toLocaleDateString());
    console.log("Three months ago, the date was " + oneMonthAgo.toLocaleDateString());
    Login or Signup to reply.
  3. Seems like you want the last day of the previous calendar month rather than something "one month ago". See details of setDate(0) here.

    const getPreviousInterval = (nextDate, months) => {
      const date = new Date(nextDate);
      date.setMonth(date.getMonth() - months + 1);
      date.setDate(0); // setDate(0) gives the last day of the previous month
      return date;
    };
    
    console.log(getPreviousInterval("2023-06-30T10:16:20.404358+00:00", 1));
    
    console.log(getPreviousInterval("2023-06-30T10:16:20.404358+00:00", 4));
    
    console.log(getPreviousInterval("2023-06-30T10:16:20.404358+00:00", 40));
    Login or Signup to reply.
  4. You can try to build a list of future dates by adding a month to the date and then one day in milliseconds each time.

    You can then use the Intl.DateTimeFormat class to format the date in British format.

    const dateFormatter = new Intl.DateTimeFormat('en-GB',  // UK date format
      { day: '2-digit', month: 'short', year: 'numeric' }); // DD MMM YYYY
    
    const formatDateRange = (fromDate, toDate) =>
      `${dateFormatter.format(fromDate)} — ${dateFormatter.format(toDate)}`;
    
    const computeFutureMonths = (startDate, months) => {
      const results = [], currDate = new Date(startDate.getTime());
      for (let i = 0; i < months; i++) {
        currDate.setMonth(currDate.getMonth() + 1);
        results.push(new Date(currDate.getTime() - 8.64e7)); // Subtract 1 day in ms
      }
      return results;
    };
    
    const startDate = new Date('2023-05-31T10:16:14+00:00');
    const nextThreeMonths = computeFutureMonths(startDate, 3);
    
    nextThreeMonths.forEach(date => {
      console.log(date.toISOString());
    });
    
    // 31 May 2023 — 30 Jun 2023
    console.log(formatDateRange(startDate, nextThreeMonths[0]));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search