我正在尝试创建一个Array of Years。当年和去年。以下代码正在运行,但是我想看看是否有更简洁的方法呢?
libm.so
答案 0 :(得分:1)
试试这个
var d = new Date();
var currentYear = d.getFullYear();
var lastYear = currentYear - 1;
$scope.years = [currentYear,lastYear];
答案 1 :(得分:0)
避免重复
var d = new Date();
var currentYear = (d.getFullYear());
var lastYear = (currentYear - 1); //Remove another call to d.getFullYear()
$scope.years = [
currentYear,
lastYear
];
如果您之后没有使用变量currentYear,您甚至可以将其缩小为:
var d = new Date();
var currentYear = (d.getFullYear());
$scope.years = [
currentYear,
currentYear - 1
];
答案 2 :(得分:0)
您可以像这样轻松获取当前年份
在您的 .ts 文件中:
public getYear() {
return new Date().getUTCFullYear();
}
然后你可以像这样在你的html文件上使用
{{getYear()}}