-1

How can I achieve this sequence of date output if the user input is 04-29-2022 and the output is like this

What I want:
2022-05-14
2022-05-29
2022-06-14
2022-06-29
2022-07-14
2022-07-29
2022-08-14
2022-08-29

my code output
2022-05-13
2022-05-28
2022-06-12
2022-06-27
2022-07-12
2022-07-27
2022-08-11
2022-08-26

var dateRelease = new Date("04-29-2022")

const terms = 8
for (let i = 0; i < terms; i++) {
  console.log(new Date(dateRelease.setDate(dateRelease.getDate() + 15)).toISOString().slice(0, 10))

}
Not a Pro
  • 163
  • 2
  • 12
  • "04-29-2022" is not a standard date string. [What are valid Date Time Strings in JavaScript?](https://stackoverflow.com/q/51715259) – VLAZ May 26 '22 at 06:01
  • ohh sorry dont mind the format the sequence of date I want to fix ohh sorry Im gonna edit my post – Not a Pro May 26 '22 at 06:03
  • What should happen in February when there is no 29th? – trincot May 26 '22 at 06:05
  • you can use [Moment](https://momentjs.com/) library for any kind of date operation in the javascript – Nikhil May 26 '22 at 06:07
  • 1
    @Nikhil, not for new developments. Even the authors of Moment discourage its use. – trincot May 26 '22 at 06:15
  • @trincot Yes bro – Not a Pro May 26 '22 at 06:18
  • Were you still planning to update your question? Any reaction to what should happen in February? – trincot May 26 '22 at 06:27
  • @trincot Pls check my question I updated it – Not a Pro May 26 '22 at 06:31
  • What if you encounter February, what should the desired output then be? Would there be 2023-02-29? – trincot May 26 '22 at 06:44
  • @trincot Actually it depends on user input . I only use that for an example – Not a Pro May 26 '22 at 06:47
  • But my question is, what *should* happen in that case,... what will be the logic you want when you arrive at an invalid date? – trincot May 26 '22 at 06:48
  • @trincot in that case it will consider to adjust the dates but until its valid the sequece will be the same – Not a Pro May 26 '22 at 06:53
  • How will you want to adjust, and what will be the consequence of the dates that follow after that correction? Please can you work out an example in your question, as this is essential to the algorithm to be proposed... I suggest you include an example when the input is 2023-01-14, and continue from there into at least March and April. – trincot May 26 '22 at 06:55
  • @trincot I can do the manual edit if needed.. If I can't do that automatically. i'm gonna use this for payment dates semi-monthly – Not a Pro May 26 '22 at 06:58
  • 1
    Why not just tell us what you expect to happen in that case ... concretely? I have been asking about this February case in my previous 6 comments, and you have not been concrete about what you want the output to be? Is there a reason why you evade that question? Is 2023-02-29 to be translated into 2023-03-01? Or to 2023-02-28? And if it is 2023-03-01, what will follow after that? 2023-03-15 or 2023-03-14? Just tell us... – trincot May 26 '22 at 07:04
  • yes 2023-02-29 will be translated into 2023-03-01 something like that if the date is not available – Not a Pro May 26 '22 at 07:13
  • 1
    And what after 2023-03-01? How will it continue? Then 2023-03-15 or 2023-03-14? Why don't you just edit your question and give examples of these kinds of boundary cases? Better be clear... – trincot May 26 '22 at 08:17

2 Answers2

1

Here is a function that takes the day of the month in the given date and determines which other date of the month would be needed in the output (either 15 more or 15 less). Then it generates dates alternating between those two date-of-the-month, and when it is the lesser of those two, incrementing the month. In case a date is invalid and automatically overflows into the next month, it is corrected to the last day of the intended month.

In case the day number from the given start date is not the desired date number for (half of) the next months in the schedule, you can pass that desired day as extra, optional argument. For instance, if the start date you give is 2023-02-28, but for the other months you prefer to use 30 as day, then you could pass 30 as argument.

To format the date in the output, it is better to not use toISODateString as that interprets the date as a UTC Date, while new Date("2022-04-29") interprets the given string as a date in the local time zone. This can lead to surprises in some time zones. So I would suggest using toLocaleDateString with a locale that produces your desired format.

Here is the code:

function createSchedule(date, count, day=0) {
    date = new Date(date);
    day ||= date.getDate();
    let k = +(day > 15);
    let days = k ? [day - 15, day] : [day, day + 15];
    let result = [];
    for (let i = 0; i < count; i++) {
        k = 1 - k;
        date.setDate(days[k]);
        // When date overflows into next month, take last day of month
        if (date.getDate() !== days[k]) date.setDate(0);         
        if (!k) date.setMonth(date.getMonth() + 1);
        result.push(date.toLocaleDateString("en-SE"));
    }
    return result;
}

var dateRelease = new Date("2022-04-29");
var result = createSchedule(dateRelease, 25);
console.log(result);

// Example using third argument
dateRelease = new Date("2023-02-28");
var result = createSchedule(dateRelease, 25, 30);
console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286
  • What if the date is new Date("2023-02-28") since february has no 29-30 .. the start of date is "2023-03-15" ,"2023-03-30" etc .. instead of "2023-03-13", "2023-03-28" – Not a Pro Mar 23 '23 at 09:12
  • I updated the answer to add support for a *desired* day number (as optional argument) for when the start date cannot have that desired day. The snippet now runs the function also with the case you mention here. – trincot Mar 23 '23 at 10:09
  • Sir is not possible to have only one argument? the code would be flexible to any input dates? – Not a Pro Mar 24 '23 at 02:39
  • How would you imagine it to know it should generate dates with 30 and not 28? – trincot Mar 24 '23 at 02:54
  • sir I guess the last day of the month is the problem is it possible that if the last day of the month it will automatically make it only add + 15 days ex if the date fall at "2023-03-31" it will automaticall move at next month and the result is 2023-04-15 etc – Not a Pro Mar 24 '23 at 03:07
  • That is not the problem. The problem is the function must have enough info to know what you want it to do. If you give it a date with day 28, you must still tell it wether you want 28 in all next months too, or 29, or 30, or 31... If you want it less generic and always want the last day of month then hardcode it. You decide... anything is possible. If you cannot make it work maybe ask a new question with all your requirements. – trincot Mar 24 '23 at 03:21
0

var dateRelease = new Date("04-29-2022")
const terms = 8
for (let i = 0; i < terms; i++) {
  let date = new Date(dateRelease.setDate(dateRelease.getDate() + 15)).toLocaleDateString('en-US');
  let format = date.split('/').map(d => d.padStart(2 ,'0')).join('-')
  console.log(format);
}