角度过滤器的日期不起作用

时间:2018-04-10 00:29:16

标签: angular angular5 typescript2.0

我想从输入框中过滤日期  但它似乎无法正常工作

日期自api数组:1979-04-08T16:00:00

我输入框中的数据:Sun Apr 08 1979 00:00:00 GMT + 0800(马来半岛标准时间)

我怎么能完成这个?

下面是我的自定义搜索过滤管道代码:

 transform(value: Beneficiary[], filter: Beneficiary): Beneficiary[] {
    var str = '0';
    if (!value || !filter) {
        console.log('no search')
        return value
    }

    if (filter.birthday) {
        //console.log(filter.birthday.getFullYear().toString() + '-' + str.concat((filter.birthday.getMonth() + 1).toString()).substr(1, 2) + '-' + str.concat((filter.birthday.getDate()).toString()).substr(1, 2))
        //console.log(str.concat((filter.birthday.getMonth() + 1).toString()).substr(1, 2));
        var searchMonth = (filter.birthday.getMonth() + 1).toString();
        var searchDate = filter.birthday.getDate().toString();
        var searchYear = filter.birthday.getFullYear().toString();
        //var completedatenew;

        if (searchMonth.length == 1) {
          searchMonth = str.concat(searchMonth);
        }

        if (searchDate.length == 1) {
          searchDate = str.concat(searchDate);
        }
        var completedate = searchYear + "-" + searchMonth + "-" + searchDate;
        console.log(completedate);
       // var completedatenew = new Date(completedate);
       // filter.birthday = completedatenew;
    }
    //console.log(Beneficiary)
    return value.filter((value: Beneficiary) => this.applyFilter(value, filter));
}


applyFilter(bene: any, filter: any): boolean {
    for (let field in filter) {
        //console.log(typeof filter[field]);
        if (filter[field]) {
            if (typeof filter[field] === 'string') {
                if (bene[field].toLowerCase().indexOf(filter[field].toLowerCase()) === -1) {
                    console.log("string false")
                    return false;
                }
            } else if (typeof filter[field] === 'number') {
                if (bene[field] !== filter[field]) {
                    console.log("number false")
                    return false;
                }
            }

            else {
                console.log(bene[field])
                console.log(filter[field])
                console.log("date false");
                return true;
            }

            //else if (typeof filter[field] === 'date') {
                //if (bene[field] !== filter[field]) {
                //    console.log("number false")
                //    return false;
                //}
            //}
        }
    }
    //console.log('returning as true')
    return true;
}

0 个答案:

没有答案