Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
How to manipulate dates for Tri-monthly payment dates using javascript

How to can I make this sequence of date with tri-monthly. for eg. the input is `"2022-03-14" the input is dynamic it depends on the user input... I'm trying add + 10 days but isn't working

The output I want

 [
      "2022-03-24",
      "2022-04-04",
      "2022-04-14",
      "2022-04-24",
      "2022-05-04",
      "2022-05-14",
      "2022-05-24",
      "2022-06-04",
      "2022-06-14",
      "2022-06-24",
    ]

My code output which is worng

[
  "2022-03-24",
  "2022-04-14",
  "2022-04-24",
  "2022-05-14",
  "2022-05-24",
  "2022-06-14",
  "2022-06-24",
  "2022-07-14",
  "2022-07-24",
  
]

function createSchedule(date, count){
date = new Date(date); 
let day = date.getDate();// Get day in given date 
let k = 0;
let days = k? [day - 10, day , day + 10] : [day, day + 10, day- 10];
let result = [];

    if(day > 10){
        k = +0
    }else{
        if(day > 20 ){
        k = +1
    }else{
        k= +2
        }
    }


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("03-14-2022");
var result = createSchedule(dateRelease, 9);
console.log(result)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A few issues in your attempt:

  • After let k = 0, the conditional operator on k? will always evaluate the first expression after ?, which is [day - 10, day , day + 10].
  • That array could have dates that are greater than 31 (day + 10)
  • That other array [day, day + 10, day- 10] is not sorted, but should be.
  • The constants +0 and +1 and +2 are OK, but it looks odd that you use the unary plus here. It could just be 0, 1 and 2.
  • The assignment k = 1 - k assumes you only have two entries in your days array, but you have three, so use modular arithmetic: k = (k + 1) % 3

Here is a correction:

function createSchedule(date, count) {
    date = new Date(date);
    let day = date.getDate();
    let firstDay = 1 + (day - 1) % 10;
    let days = [firstDay, firstDay + 10, firstDay + 20];
    let k = days.indexOf(day);
    let result = [];
    for (let i = 0; i < count; i++) {
        k = (k + 1) % 3;
        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-03-14");
var result = createSchedule(dateRelease, 25);
console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!