使用 TypeScript 检查一个人今年是否达到特定年龄 (JavaScript)
Checking if a person is turning a specific age this year with TypeScript (JavaScript)
我认为我的问题很容易解决,但我就是无法掌握它的窍门。
我想要一个函数,它检查此人在当年是否年满 18 岁,但此人在检查时不必已经 18 岁。
示例:
"Mike turns 18 next week"。所以他今天还是 17 岁,但函数应该 return 为真,因为他今年将满 18 岁。如果 Mike 明年年满 18 岁,该函数应该 return false。
这是我目前得到的,但是这个函数只检查这个人是否已经 18 岁。
dateString = "2000-02-23" // person is 17 but turns 18 next week, so function should return true
otherDateString = "2001-01-13" //person is also 17, but function should return false, because this person just turnt 17 this year and will turn 18 next year
calculateAge(dateString) {
let birthday = new Date(dateString);
let ageDifMs = Date.now() - birthday.getTime();
let ageDate = new Date(ageDifMs); // milliseconds from epoch
let age = Math.abs(ageDate.getFullYear() - 1970);
console.log(age);
if (age>=18)
return true;
}
我如何才能将功能更改为只检查此人今年是否年满 18 岁,而不管此人是否仍未满 17 岁?
这样做应该很容易:
dateString = "2000-02-23" // person is 17 but turns 18 next week, so function should return true
otherDateString = "2001-01-13" //person is also 17, but function should return false, because this person just turnt 17 this year and will turn 18 next year
function calculateAge(dateString) {
let birthday = new Date(dateString);
let now = new Date();
return now.getYear() - birthday.getYear() >= 18;
}
您不需要将输入的日期字符串解析为日期,事实上,如果您不这样做会更好。参见 Why does Date.parse give incorrect results?
您需要做的就是从日期字符串中获取年份,然后从当前年份中减去它。如果结果是 18,那么他们今年就满 18 岁了:
function eighteenThisYear(s) {
return new Date().getFullYear() - s.split(/\D/)[0] == 18;
}
['1999-12-31','2000-02-23','2001-02-03'].forEach(function(d) {
console.log(d + ': ' + eighteenThisYear(d));
});
我认为我的问题很容易解决,但我就是无法掌握它的窍门。 我想要一个函数,它检查此人在当年是否年满 18 岁,但此人在检查时不必已经 18 岁。
示例: "Mike turns 18 next week"。所以他今天还是 17 岁,但函数应该 return 为真,因为他今年将满 18 岁。如果 Mike 明年年满 18 岁,该函数应该 return false。
这是我目前得到的,但是这个函数只检查这个人是否已经 18 岁。
dateString = "2000-02-23" // person is 17 but turns 18 next week, so function should return true
otherDateString = "2001-01-13" //person is also 17, but function should return false, because this person just turnt 17 this year and will turn 18 next year
calculateAge(dateString) {
let birthday = new Date(dateString);
let ageDifMs = Date.now() - birthday.getTime();
let ageDate = new Date(ageDifMs); // milliseconds from epoch
let age = Math.abs(ageDate.getFullYear() - 1970);
console.log(age);
if (age>=18)
return true;
}
我如何才能将功能更改为只检查此人今年是否年满 18 岁,而不管此人是否仍未满 17 岁?
这样做应该很容易:
dateString = "2000-02-23" // person is 17 but turns 18 next week, so function should return true
otherDateString = "2001-01-13" //person is also 17, but function should return false, because this person just turnt 17 this year and will turn 18 next year
function calculateAge(dateString) {
let birthday = new Date(dateString);
let now = new Date();
return now.getYear() - birthday.getYear() >= 18;
}
您不需要将输入的日期字符串解析为日期,事实上,如果您不这样做会更好。参见 Why does Date.parse give incorrect results?
您需要做的就是从日期字符串中获取年份,然后从当前年份中减去它。如果结果是 18,那么他们今年就满 18 岁了:
function eighteenThisYear(s) {
return new Date().getFullYear() - s.split(/\D/)[0] == 18;
}
['1999-12-31','2000-02-23','2001-02-03'].forEach(function(d) {
console.log(d + ': ' + eighteenThisYear(d));
});