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

220
Views
Moment .isAfter not returning correctly while comparing two dates

I've got a problem with fucntion .isAfter(), when it comes to compare "05/04/2022" with "03/05/2022" it tells me that 05/04/2022>03/05/2022 I specified that I want to compare the whole date by adding 'day' in argument but nothing changes.. Can someone help me please ? Here is my code :

let date1 = moment("03-05-2022", "DD-MM-YYYY").format("DD-MM-YYYY");
data.forEach(d => {
  let test = date1;
  let date = moment(d.received_on).format("DD-MM-YYYY");
  const {
    adc_v,
  } = d;

  let dateIsAfter = moment(date).isAfter(moment(date1), 'day');
  let dateIsSame = moment(date).isSame(moment(date1), 'day');


  // const dateIsAfter = moment(date) > moment(date1);
  // const dateIsSame = moment(date) == moment(date1);


  if (dateIsSame === true) {
    this.arrTension.push({
      date,
      value: adc_v,
    });
    console.log(date + '  : ' + adc_v);
    date1 = moment(date1, "DD-MM-YYYY").add(1, 'days').format('DD-MM-YYYY');
  }
  if (dateIsAfter === true) {
    console.log('date = ' + date);
    console.log('date1 = ' + date1);
    date1 = moment(date1, "DD-MM-YYYY").add(1, 'days').format('DD-MM-YYYY');
  }
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is happening because you have not specified your locale.

When it compares 05/04/2022>03/05/2022, it is essentially checking 4th May 2022 is greater that 5th March 2022.

Specify the global locale as follows:

moment.locale('fr')

Then compare the dates as you were before:

moment(date).isAfter(moment(date1), 'day');

about 4 years ago · Juan Pablo Isaza Report

0

I changed the format and now it is working well =)

enter code herelet date1 = moment("05-03-2021", "MM-DD-YYYY").format("MM-DD-YYYY");
                    console.log(date1);
                    data.forEach(d => {
                        let test = date1;
                        let date = moment(d.received_on).format("MM-DD-YYYY");
                        const {
                            adc_v,
                        } = d;
                        
                        let dateIsAfter = moment(date).locale('fr').isAfter(moment(date1), 'day');
                        let dateIsSame = moment(date).locale('fr').isSame(moment(date1), 'day');


                        // const dateIsAfter = moment(date) > moment(date1);
                        // const dateIsSame = moment(date) == moment(date1);
                        
                        
                            if (dateIsSame === true ){
                                this.arrTension.push({
                                    date,
                                    value: adc_v,
                                });
                                console.log(date + '  : ' + adc_v);
                                date1 = moment(date1, "MM-DD-YYYY").locale('fr').add(1, 'days').format('MM-DD-YYYY');
                            }                       
                            if (dateIsAfter === true ){
                                console.log('date = ' + date);
                                console.log('date1 = '  + date1);
                                date1 = moment(date1, "MM-DD-YYYY").locale('fr').add(1, 'days').format('MM-DD-YYYY');
                            }       
                    });
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!