日期计算错误
我试图带某人的生日,找到他/她年满65岁的一天。我有以下代码,但计算似乎关闭了。例如,如果我使用生日11/15/1957,我应该将11/15/2022作为65岁生日。但是,我得到的是1982年8月9日(这是不正确的)。
我想念什么吗?
function convertDate(birthday) {
var usethisdate = new Date(birthday);
//let's start by finding the person's 65th birthday
var birthdaysixtyfive = new Date(usethisdate.setMonth(usethisdate.getMonth() + 780));
console.log("65th birthday: ", birthdaysixtyfive.toDateString());
}
convertDate('11/15/1957');
I'm trying to take someone's birthday and find the day that he/she turns 65. I have the following code but the calculation seems off. For example, if I use the birthday 11/15/1957 I should get 11/15/2022 as the 65th birthday. However, I'm getting 8/9/1982 (which is incorrect) instead.
Am I missing something?
function convertDate(birthday) {
var usethisdate = new Date(birthday);
//let's start by finding the person's 65th birthday
var birthdaysixtyfive = new Date(usethisdate.setMonth(usethisdate.getMonth() + 780));
console.log("65th birthday: ", birthdaysixtyfive.toDateString());
}
convertDate('11/15/1957');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需在提供的年份中加65个即可。您会得到结果。下面的代码应完成工作。
Just add 65 to the provided year. And you'll get the result. Below code should do the work.
你的代码没问题。我想你想要这样的格式:
谢谢。
Your code is ok. I think you want like this format :
Thanks.
我建议使用“momentjs”库。它具有求差的功能。
I would recommend using the 'momentjs' library. It has a function for difference.