最近写angular2项目,时间显示时,有这个要求
在ngOnInit()钩子函数里调用方法
async ngOnInit() {
const today = new Date();
const str = '7123456'.charAt(new Date().getDay());
// 这周开始时间 xxxx-xx-xx
const date1 = this.getDay(- Number(str) + 1);
// 这周结束时间
const date2 = this.getDay(7 - Number(str));
// 当月开始
const startDate = this.CurrentMonthFirst(today);
// 当月最后一天
const endDate = this.CurrentMonthLast(today);
}
实现函数
// 显示最近一周的时间
getDay(day) {
const today = new Date();
const time = today.getTime() + 1000 * 60 * 60 * 24 * day;
// 注意,这行是关键代码
today.setTime(time);
const tYear = today.getFullYear();
let tMonth = today.getMonth();
let tDate = today.getDate();
tMonth = this.doHandleMonth(tMonth + 1);
tDate = this.doHandleMonth(tDate);
return tYear +