-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
  • 1
  • 8
  • "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 Okay, Thanks for the updated info. – Nikhil May 26 '22 at 06:28
  • @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.

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) {
    date = new Date(date);
    let 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);
trincot
  • 263,463
  • 30
  • 215
  • 251
  • WOW!! This is exactly what I'm looking for ! Thanks a lot bro u help me on my project! ..I appreciate your help – Not a Pro May 26 '22 at 09:29
  • bro How about tri-monthly same with this repeating days ? – Not a Pro May 31 '22 at 06:37
  • That would be different. You'd have to make sure to store the desired days in the `days` array and initialise `k` to point to the right one to start with, and then make `k` walk cyclic through that array. If you cannot make it work, I suggest you ask a question about it, showing your efforts. – trincot May 31 '22 at 06:43
  • Actually yesterday until now I'm trying to tweak ur code to make it tri-monthly It's hard for me to solve it .. I'm gonna post another question – Not a Pro May 31 '22 at 06:47
  • https://stackoverflow.com/questions/72444648/how-to-manipulate-dates-for-tri-monthly-payment-dates-using-javascript I already posted my question – Not a Pro May 31 '22 at 08:39
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);
}