繁体   English   中英

如何从打字稿中的字符串中减去Day?

[英]How to subtract Day from string in typescript?

我需要帮助从endDate减去一天-但是endDate是字符串。

尝试与此:

moment(this.endDate).subtract(1, 'day').format()

不起作用。

 public ngOnChanges(): void {
    const startDate = this.startDate ? moment(this.startDate).format() : moment().startOf('day').format();
    const endDate = this.endDate ? moment(this.endDate).format() : moment(startDate).endOf('day').format();
    this.datePicker = new DatePicker(startDate, endDate);
    this.setDatePicker();

    if (this.continiousDatesValue !== null) {
        this.continiousDatesValueMoment = moment(this.continiousDatesValue);
    }
    if (this.previousDatesValue !== null) {
        this.previousDatesValueMoment = moment(this.previousDatesValue);
    }
}

同样在这里,我应该从“ endDate”中减去一天-这是字符串:

private onDatesChange(): void {
        this.startDateChange.emit(this.datePicker.startDate);
        this.endDateChange.emit(this.datePicker.endDate);
        this.startEndDateChange.emit({startDate: this.datePicker.startDate, endDate: this.datePicker.endDate});
        this.isOpen = false;
    }

将减去的日期设置为日期对象

 let endDate = "2018-04-01 08:14:00"; let dateOb = new Date(endDate); dateOb.setDate(dateOb.getDate() - 1); console.log(dateOb); // now you can use moment to parse to your desired format //or endDate = dateOb.getFullYear() + "-" + ('0' + (dateOb.getMonth() + 1)).slice(-2) + "-" + ('0' + dateOb.getDate()).slice(-2) + " " + ('0' + dateOb.getHours()).slice(-2) + ":" + ('0' + dateOb.getMinutes()).slice(-2) + ":" + ( '0' + dateOb.getSeconds()).slice(-2); // getMonth() starts from 0. console.log(endDate) 

moment.js期望以MM DD YYYY格式或YYYY MM DD的日期进行操作。 因此,以类似的格式提供日期,或者您可以使用Ashish Ranjan提出的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM