Javascript:计算从出生年到今天的天,小时,分钟,秒

时间:2017-10-16 03:54:18

标签: javascript

在我的程序中通过提示值我计算出生年份,下一个转折年份,天数,小时数,分钟数和秒数。现在我想计算这个人从出生日起活了多久。为此,我做了以下计算,但我对多少天,小时,分钟,秒的活动计算显示不正确。有人可以帮忙吗?

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

var today = new Date();

var myMonth = prompt("What month were you born in?");
var myDay = prompt("What day were you born on?");
var myYear = prompt("What year were you born in?");

var birthday = new Date(myYear, myMonth, myDay);

var dateLoc = document.getElementById("dateField").innerHTML = "Today is " + today;

var birthField = document.getElementById("birthField")
  .innerHTML = "I was born on " + monthNames[myMonth - 1] + ' ' + myDay + ', ' + myYear + '.' +
  '(' + myMonth + '/' + myDay + '/' + myYear + ')';

var milliseconds, seconds, minutes, hours, days;

var years = today.getFullYear() - birthday.getFullYear();

myMonth--;
var nextBirthday = (new Date(today.getFullYear(), myMonth, myDay)) < today ? (new Date(today.getFullYear() + 1, myMonth, myDay)) : (new Date(today.getFullYear(), myMonth, myDay))

console.log(nextBirthday);

milliseconds = diff % 1000;
diff = (diff - (milliseconds)) / 1000;

seconds = diff % 60

diff = (diff - seconds) / 60;

minutes = diff % 60
diff = (diff - minutes) / 60;

hours = diff % 24
days = (diff - hours) / 24;

var nextBirth = document.getElementById("nextBirth").innerHTML = "I am " + years + "  Years old, and will turn " + (years + 1) + " in:" + "<br>" + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";

milliseconds = diffAlive % 1000;
diffAlive = (diffAlive - (milliseconds)) / 1000;

seconds = diffAlive % 60

diffAlive = (diffAlive - seconds) / 60;

minutes = diffAlive % 60
diffAlive = (diffAlive - minutes) / 60;

hours = diffAlive % 24
days = (diffAlive - hours) / 24;


var aliveFor = document.getElementById("aliveFor").innerHTML = "I have been alive for..." + "<br>" + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
p {
  text-align: center;
  font-family: monospace;
  font-size: 20px;
}

body {
  background-image: url(birth.jpg");
  background-repeat: no-repeat;
  background-size: 1300px 600px;
}
<p id="dateField"> </p>
<p id="birthField"> </p>
<p id="nextBirth"> </p>
<p id="aliveFor"> </p>

输出:

enter image description here

3 个答案:

答案 0 :(得分:1)

在计算birthday时,您错误地将月份传递给Date构造函数。它基于0。

在设置生日变量之前将myMonth--行移至。

e.g。

myMonth--;
var birthday = new Date(myYear, myMonth, myDay);

答案 1 :(得分:1)

你的&#34; var生日&#34;计算了一个月前的例子,如果&#34; 10月&#34;通过提示输入生日变量是11月。因此,请参考以下代码段并进行更改。希望这有帮助:)

    

<head>
    <style>
    p {
        text-align: center;
        font-family: monospace;
        font-size: 20px;
    }

    body {

        background-image: url("birth.jpg");
        background-repeat: no-repeat;
        background-size: 1300px 600px;
    }
    </style>
    <title>program</title>
</head>

<body>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    <p id="dateField"> </p>
    <p id="birthField"> </p>
    <p id="nextBirth"> </p>
    <p id="aliveFor"> </p>
    <script>
    var monthNames = ["January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    ];

    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    var today = new Date();

    var myMonth = prompt("What month were you born in?");
    var myDay = prompt("What day were you born on?");
    var myYear = prompt("What year were you born in?");



    // var birthday = new Date(myYear, myMonth, myDay);

    // this change will make it work perfectly fine
    var birthd = myYear + " " + myMonth + " " + myDay;

    var birthday = new Date(birthd);

    var dateLoc = document.getElementById("dateField").innerHTML = "Today is " + today;

    var birthField = document.getElementById("birthField")
        .innerHTML = "I was born on " + monthNames[myMonth - 1] + ' ' + myDay + ', ' + myYear + '.' +
        '(' + myMonth + '/' + myDay + '/' + myYear + ')';

    var milliseconds, seconds, minutes, hours, days;

    var years = today.getFullYear() - birthday.getFullYear();

    myMonth--;
    var nextBirthday = (new Date(today.getFullYear(), myMonth, myDay)) < today ? (new Date(today.getFullYear() + 1, myMonth, myDay)) : (new Date(today.getFullYear(), myMonth, myDay))

    console.log(nextBirthday);

    var diff = nextBirthday - today;

    milliseconds = diff % 1000;
    diff = (diff - (milliseconds)) / 1000;

    seconds = diff % 60

    diff = (diff - seconds) / 60;

    minutes = diff % 60
    diff = (diff - minutes) / 60;

    hours = diff % 24
    days = (diff - hours) / 24;

    var nextBirth = document.getElementById("nextBirth").innerHTML = "I am " + years + "  Years old, and will turn " + (years + 1) + " in:" + "<br>" + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";


    var diffAlive = today - birthday;

    milliseconds = diffAlive % 1000;
    diffAlive = (diffAlive - (milliseconds)) / 1000;

    seconds = diffAlive % 60

    diffAlive = (diffAlive - seconds) / 60;

    minutes = diffAlive % 60
    diffAlive = (diffAlive - minutes) / 60;

    hours = diffAlive % 24
    days = (diffAlive - hours) / 24;


    var aliveFor = document.getElementById("aliveFor").innerHTML = "I have been alive for..." + "<br>" + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
    </script>
</body>

</html>

答案 2 :(得分:1)

计算时差是一项棘手的任务,因此您最好使用某些库,例如moment.js

要使用此库解决您的任务,您需要执行以下操作:

// Make two dates
var yearOfBirth = moment([2007, 0, 29]);
var now = moment();

// Calculate the difference in seconds
var difference = now.diff(yearOfBirth) 

// Make duration object
var duration = moment.duration(difference);

// Now you can format duration object as you want to, see the docs
alert(duration.years());