生日显示为去年的年龄? [英] Birthday showing as previous year's age?

查看:65
本文介绍了生日显示为去年的年龄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我想要使用它的网站测试JavaScript代码段。基本上,当页面加载我的年龄执行的功能时。我是在一个固定的出生日期做这件事。我在使用birthDate变量时发现了一个错误(不确定它究竟发生的原因)。我的错误发生在birthDate月比当前月少一个,并且该日至少比当前日期一直到当前日期一天(例如:今天的日期6月8日,任何从5月9日到6月8日的产生错误)

I am testing a JavaScript snippet out for a site I want to use it in. Basically when the page loads the function my age executes. I'm doing this off a set birth-date. I noticed an error when playing with the birthDate variable (not sure exactly why it happens). My error happening when the birthDate month is one less than the current month, and the day is at least one greater than the current day all the way to the current date (Ex: Today's Date June 8th, anything from May 9th to June 8th produces the error)

对于该片段,我输入了出生日期5-9-1989。现在,当你或我做数学时,我们都知道基于今天的日期6-8-2015。年龄应为26.但由于某种原因,代码吐出数字25,如下所示。

For the snippet I entered the birth date 5-9-1989. Now when you or I do the math we both know that based on today's date 6-8-2015. The age should be 26. But for some reason the code spits out the number 25 as demonstrated below.

(注意:只要月份的birthDate变量输入为1小于当月,并且当天至少比当天更大一年,错误将发生一年并不重要。如果日期晚于或早于当月 - 1和天+ n(n> 0)到现在日期没有发生错误)

(Note: as long as the birthDate variable input of month is one less than the current month, and the day is at least one greater than the current day the error will happen year doesn't matter. If the dates are later or earlier then month - 1 and day + n (n > 0) to present date the error doesn't occur)

任何帮助搞清楚这个错误发生的原因都会非常明显。

Any help figuring out why this error is occuring would be greatly appericated.

function myage() {
var birthDate = new Date(1989, 5, 9, 0, 0, 0, 0)

// The current date
var currentDate = new Date();

// The age in years
var age = currentDate.getFullYear() - birthDate.getFullYear();

// Compare the months
var month = currentDate.getMonth() - birthDate.getMonth();

// Compare the days
var day = currentDate.getDate() - birthDate.getDate();

// If the date has already happened this year
if ( month < 0 || month == 0 && day < 0 || month < 0 && day < 0 )
{
    age--;
}

document.write('I am ' + age + ' years old.');
}

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body onLoad="myage()">
</body>
</html>

推荐答案

很简单, Date 函数的月份是指在0(1月)到11(12月)的范围内给出。只需将5更改为4即可指定5月。

Quite simply, the month for the Date function is meant to be given within a range of 0 (January) to 11 (December). Just change the 5 to a 4 to specify May.

引用 MDN




表示月份的整数值,从1月份的0开始到12月份的11月。

month
Integer value representing the month, beginning with 0 for January to 11 for December.

function myage() {
var birthDate = new Date(1989, 4, 9, 0, 0, 0, 0)

// The current date
var currentDate = new Date();

// The age in years
var age = currentDate.getFullYear() - birthDate.getFullYear();

// Compare the months
var month = currentDate.getMonth() - birthDate.getMonth();

// Compare the days
var day = currentDate.getDate() - birthDate.getDate();

// If the date has already happened this year
if ( month < 0 || month == 0 && day < 0 || month < 0 && day < 0 )
{
    age--;
}

document.write('I am ' + age + ' years old.');
}

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body onLoad="myage()">
</body>
</html>

这篇关于生日显示为去年的年龄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆