0

Update:

I have several objects with start dates and end date

When inserting / modifying an object, the starting or ending date of the new object can not be included in the existing object.

 Exist : 06/06/2018-----30/06/2018

can input:

04/06/2018-------05/06/2018
02/02/2018------ until less than date Start of which exists (05/06/2018)
or higher: 
31/06/2018--------

can not get in :

 04/06/2018-------07/06/2018 The end is already understood between the beginning and the end exists.

Or

07/06/2018---08/06/2018 because it is also included between the beginning and the end of the.

Code:

 validateParamsDates(url, newConfig) {

return this.http.get<any>(url).pipe(map(param => {

  let messageError = { message: "", showAlert: false };

  let userStart = newConfig.startdatevalidity;
  let userFinish = newConfig.enddatevalidity;

  param[1]['params'].array.forEach(element => {

    if(userStart > element.startdatevalidity && userFinish > element.enddatevalidity
       || userStart <  element.startdatevalidity && userFinish < element.enddatevalidity
       && userStart > element.enddatevalidity
      ){
        console.log('good');
    }else{
     console.log('=(');
    }

  });

  return messageError  ;

}));

}

EduBw
  • 866
  • 5
  • 20
  • 40

2 Answers2

1

You should first convert the strings to a Date object. Then you can compare dates and everything will work as it should ;)

So you would do something like:

const start2: Date = new Date(Object2.start);
const end1: Date = new Date(Object1.end);

if (start2 > end1) { console.log('good'); } 

Please also note that in order for all of this to work in javascript, the dates should be defined in MM/DD/YYYY format not in DD/MM/YYYY

l.varga
  • 851
  • 6
  • 14
  • How can I compare dates ? – EduBw Jun 27 '18 at 10:20
  • I need know If date2 is contain en date1, because obj1 start-finish in 1/5/2018-31/5/2018, and Object2 start/finish 1/2/2018-31/2/2018. THEN object2 IS CORRECTS – EduBw Jun 27 '18 at 10:26
  • 1
    @EduBw well, that's simple. You need to check if (date2.end is less than date1.start and if date2.start is less than date2.end), or if (date2.start is greater than date1.end and if date2.end is greater than date2.start).. with all of the dates being correctly converted in `Date` objects, of course – l.varga Jun 27 '18 at 10:31
  • No problem. If the answer was helpful or solved your issue, please mark it as such :) – l.varga Jun 27 '18 at 10:41
0

I would probably do something like this.

var start = new Date(Object2.start);
var end = new Date(Object1.end);

if(start.getTime() > end.getTime()) console.log('good');

Similar to this answer: Compare two dates with JavaScript