首页 > 解决方案 > TypeScript 中的数据范围

问题描述

更新:

我有几个具有开始日期和结束日期的对象

插入/修改对象时,新对象的开始或结束日期不能包含在现有对象中。

 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.

代码:

 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  ;

}));

}

标签: javascriptangulartypescript

解决方案


您应该首先将字符串转换为Date对象。然后你可以比较日期,一切都会正常运行;)

所以你会做这样的事情:

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

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

另请注意,为了让所有这些都在 javascript 中工作,日期应该以MM/DD/YYYY格式定义,而不是DD/MM/YYYY


推荐阅读