Home > OS >  Moment .isAfter not returning correctly while comparing two dates
Moment .isAfter not returning correctly while comparing two dates

Time:05-12

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');
                                }       
                        });``

CodePudding user response:

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');

CodePudding user response:

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');
                            }       
                    });
  • Related