日期计算错误

发布于 2025-01-19 11:40:43 字数 543 浏览 0 评论 0原文

我试图带某人的生日,找到他/她年满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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪 2025-01-26 11:40:43

只需在提供的年份中加65个即可。您会得到结果。下面的代码应完成工作。

let birthDay = new Date('11/15/1957')

function getDay(birthDay){
  let birthYear = birthDay.getFullYear() + 65
  let birthArr = birthDay.toDateString().split(' ')
  let calculatedDay = new Date(`${birthYear}-${birthArr[1]}-${birthArr[2]}`).toDateString()
  return calculatedDay
}

console.log(getDay(birthDay))

Just add 65 to the provided year. And you'll get the result. Below code should do the work.

let birthDay = new Date('11/15/1957')

function getDay(birthDay){
  let birthYear = birthDay.getFullYear() + 65
  let birthArr = birthDay.toDateString().split(' ')
  let calculatedDay = new Date(`${birthYear}-${birthArr[1]}-${birthArr[2]}`).toDateString()
  return calculatedDay
}

console.log(getDay(birthDay))

从此见与不见 2025-01-26 11:40:43

你的代码没问题。我想你想要这样的格式:

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));

    birthdaysixtyfive

    bd65 = (birthdaysixtyfive.getMonth() + 1)+'/'+birthdaysixtyfive.getDate()+'/'+birthdaysixtyfive.getFullYear();
    
    console.log("65th birthday: ",bd65);
}


convertDate('11/15/1957');

谢谢。

Your code is ok. I think you want like this format :

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));

    birthdaysixtyfive

    bd65 = (birthdaysixtyfive.getMonth() + 1)+'/'+birthdaysixtyfive.getDate()+'/'+birthdaysixtyfive.getFullYear();
    
    console.log("65th birthday: ",bd65);
}


convertDate('11/15/1957');

Thanks.

扭转时空 2025-01-26 11:40:43

我建议使用“momentjs”库。它具有求差的功能。

const moment = require('moment')
function convertDate(birthday) {

    var usethisdate = new Date(birthday);
    
    //let's start by finding the person's 65th birthday
    
    var age  = moment().diff(usethisdate, 'years');
    console.log("Age: ",age);
    var currentDate = moment();
    var year = moment(currentDate).add(age, 'Y').format('DD-MM-YYYY'); //or replace age with 65
    console.log("Xth birthday: ",year);
    }

    convertDate(11/15/1957)

I would recommend using the 'momentjs' library. It has a function for difference.

const moment = require('moment')
function convertDate(birthday) {

    var usethisdate = new Date(birthday);
    
    //let's start by finding the person's 65th birthday
    
    var age  = moment().diff(usethisdate, 'years');
    console.log("Age: ",age);
    var currentDate = moment();
    var year = moment(currentDate).add(age, 'Y').format('DD-MM-YYYY'); //or replace age with 65
    console.log("Xth birthday: ",year);
    }

    convertDate(11/15/1957)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文