【问题标题】:Age Reverse Calculation Using Javascript使用 Javascript 进行年龄反向计算
【发布时间】:2020-04-03 11:33:57
【问题描述】:

我想从日、月、年计算年龄。我已经完成了出生日期到日、月、年的计算。但是我想要年龄反向计算

如果用户输入年龄: 天:18 月:05 年份:26

然后它将返回当前日期的原始出生日期,例如 DOB:10/24/1993

有很多出生日期到年龄(dd-mm-yyyy)的例子,但没有年龄(dd-mm-yyyy)到出生日期的例子

javascript中这个反向计算的逻辑是什么?

这是 DOB 到年龄的脚本

$("#txtDob").keyup(function () {
                debugger;
                var mdate = $("#txtDob").val().toString();
                var yearThen = parseInt(mdate.substring(0, 4), 10);
                var monthThen = parseInt(mdate.substring(5, 7), 10);
                var dayThen = parseInt(mdate.substring(8, 10), 10);

                var today = new Date();
                var birthday = new Date(yearThen, monthThen - 1, dayThen);

                var differenceInMilisecond = today.valueOf() - birthday.valueOf();

                var year_age = Math.floor(differenceInMilisecond / 31536000000);
                var day_age = Math.floor((differenceInMilisecond % 31536000000) / 86400000);

                var month_age = Math.floor(day_age / 30);

                day_age = day_age % 30;

                if (isNaN(year_age) || isNaN(month_age) || isNaN(day_age)) {
                    // $("#exact_age").text("Invalid birthday - Please try again!");
                }
                else {
                    $("#txtAgeYY").val(year_age);
                    $("#txtAgeMM").val(month_age);
                    $("#txtAgeDD").val(day_age);
                    //var abc = testFunc();
                }
            });

但我需要年龄才能出生

【问题讨论】:

  • 请展示你的脚本
  • 不显示脚本很难回答..
  • 我将 DOB 的脚本添加到 Age,但我希望 Age 从当前日期开始 DOB。请你们告诉我逻辑。
  • 该脚本似乎以天、月和年为单位计算用户的年龄。 txtAgeYY、txtAgeMM 和 txtAgeDD 不打算分别输入出生日期。其次 - 它将#txtDob 视为 RTL 输入。我想说你甚至没有尝试测试你发布的代码块

标签: javascript


【解决方案1】:

您可以创建一个包含当前时间的新日期,然后减去当前年龄的年月日。

const ageYear = 26;
const ageMonth = 5;
const ageDay = 18;

const birthDay = new Date();

birthDay.setFullYear(birthDay.getFullYear() - ageYear);
birthDay.setMonth(birthDay.getMonth() - ageMonth);
birthDay.setDate(birthDay.getDate() - ageDay);

【讨论】:

  • 但是返回错误的出生日期,我认为闰年计算不正确
  • 您计算年龄的示例没有正确计算年龄。您不应该使用毫秒差然后除以31536000000。正如你所说的闰年,这些年份的毫秒数不同。
  • 尝试计算一个人 26 岁并出生于 1993 年 10 月 24 日的确切时间。他在 2019 年 10 月 24 日满 26 岁,对吧?你的算法给出了 26 年零 6 天。 ` const current = new Date(2019, 10, 24); const 出生 = 新日期 (1993, 10, 24); const diff = current.valueOf() -birth.valueOf(); const year_age = Math.floor(diff / 31536000000); const day_age = Math.floor((diff % 31536000000) / 86400000); console.log(year_age, day_age) `
  • 我试过你的代码,它显示的是 10/16/1993,它应该是 10/24/1993。你能正确编辑你的代码吗,拜托。
  • 今天我们在4/3/2020,年龄是26岁5个月18天。从现在起减去 18 天 => 2020 年 3 月 16 日减去 5 个月 => 2019 年 10 月 16 日减去 26 年 => 1993 年 10 月 16 日而不是 1993 年 10 月 24 日。您从 BOD 计算日期的算法是错误的。
【解决方案2】:

检查下面的javascript部分会做你想要的。

<!DOCTYPE html>
<html>
 <body>

  <p id="demo"></p>

  <script>
   var ageYears = 26;
   var ageMonths = 5;
   var ageDays = 18;
   var today = new Date();
   var year = today.getFullYear();
   var month = today.getMonth();
   var day = today.getDate();
   var dob = new Date(year - ageYears, month - ageMonths, day - ageDays);
   document.getElementById("demo").innerHTML = "DOB: " + dob;
 </script>

</body>

【讨论】:

  • 它还返回 1993 年 10 月 16 日星期六 00:00:00 GMT+0530(印度标准时间),而不是 1993 年 10 月 24 日星期六 00:00:00 GMT+0530(印度标准时间):(
  • 你们是对的,实际上我的出生日期到年龄的逻辑是不正确的。谢谢:)
  • @Vidhya 请根据出生日期检查您的年龄计算是否正确。我有 26 年 5 个月零 9 天。
猜你喜欢
  • 2016-03-20
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
相关资源
最近更新 更多