【发布时间】:2018-04-13 11:56:50
【问题描述】:
在我的 Angular 5 项目中,我需要显示当月的最后日期。我尝试了几种技术,但都没有给我正确的答案。尽管有适用于 JavaScript 的解决方案,但不知何故它在 TypeScript 中不起作用。
HTML:
<button class = "btn btn-sm btn-outline primary" (click) = "selectThisMonth()" >This Month</button>
<pre> Last Date of Month: {{ toDate | json }} </pre>
打字稿:
import { Component } from '@angular/core';
import {NgbDateStruct, NgbCalendar} from '@ng-bootstrap/ng-bootstrap';
const now = new Date();
@Component({
selector: 'app',
templateUrl: 'app.html'
})
export class AppComponent {
fromDate: NgbDateStruct;
toDate: NgbDateStruct;
constructor (calendar: NgbCalendar){
this.fromDate = calendar.getToday();
this.toDate = calendar.getNext(calendar.getToday(), 'd', 10);
}
selectThisMonth() {
this.toDate = {year: now.getFullYear(), month: now.getMonth+1, day: now.getDate()};
}
}
如果 TypeScript 有任何可行的解决方案,请提出建议。
预期输出应显示“2018 年 4 月 30 日”,因为 30 是当月的最后一个日期。
【问题讨论】:
标签: angular typescript datepicker angular5