我正在嘗試過某人的生日并找到他/她 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);
}
uj5u.com熱心網友回復:
只需將 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))
uj5u.com熱心網友回復:
我建議使用“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)
uj5u.com熱心網友回復:
你的代碼沒問題。我想你想要這種格式:
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');
謝謝。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/457283.html
標籤:javascript 日期
上一篇:`javascript`我想將每個物件的不同陣列值插入到物件內部的陣列中
下一篇:如何制作近距離影片?
