我正在尝试执行一个返回weekNumber的函数,并且在最后一行显示错误, “算术运算的左右手必须输入任何数字或枚举”
有什么解决办法吗?
weekNumber(d: Date) {
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
// set thursday: current date + 4 - current day
// make sunday number 7
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
// first day of year
const firstDayOfYear = new Date(Date.UTC(d.getFullYear(), 0 , 1 ));
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0 , 1 ));
firstDayOfYear.getFullYear();
// calculate full weeks to nearest thrusday
const weekNo = Math.ceil(( ((d - yearStart) / 86400000) + 1) / 7);
}
答案 0 :(得分:1)
您可以使用any
解决此问题
weekNumber(d: any) {
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
// set thursday: current date + 4 - current day
// make sunday number 7
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
// first day of year
const firstDayOfYear = new Date(Date.UTC(d.getFullYear(), 0, 1));
const yearStart = <any>new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
firstDayOfYear.getFullYear();
// calculate full weeks to nearest thrusday
const weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
}
或其他解决方案
您可以使用moment
解决此问题
安装moment
库
npm install moment --save
import moment from 'moment';
export class AppComponent {
weekNumber(d: Date) {
const weekNo = moment(d).week()
}
}