0

I am trying to work out how old a user is based on the selection statement.

I cannot calculate the current (year/month/day/hour/minute) from the user input.

The intent is that the user will see 3 box to select [Month] [Day] and [Year] of birth.

Based on the answer I want to calculate the current date minus the date selected by the user.

For some reason I can't make the logic work. I can hard code it, but I want it to be logical.

Here is my code:

<!DOCTYPE>
<html>
<title>Validate Credit Cards</title>
<head>
    <script type="text/javascript">
    /* <![CDATA[ */
    /* ]]> */
    </script>
</head>
<body>
    <form action="">
            <h1>Age Calculator</h1>
            <p>Birth Date:
                <select name="month">
                    <option>Month</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                </select>
                <select name="day">
                    <option>Day</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                    <option>13</option>
                    <option>14</option>
                    <option>15</option>
                    <option>16</option>
                    <option>17</option>
                    <option>18</option>
                    <option>19</option>
                        <option>20</option>
                    <option>21</option>
                    <option>22</option>
                    <option>23</option>
                    <option>24</option>
                    <option>25</option>
                    <option>26</option>
                <option>27</option>
                <option>28</option>
                <option>29</option>
                <option>30</option>
                <option>31</option>
            </select>
            <select name="year">
                <option>Year</option>
                <option>1950</option>
                <option>1951</option>
                <option>1952</option>
                <option>1953</option>
                <option>1954</option>
                <option>1955</option>
                <option>1956</option>
                <option>1957</option>
                <option>1958</option>
                <option>1959</option>
                <option>1960</option>
                <option>1961</option>
                <option>1962</option>
                <option>1963</option>
                <option>1964</option>
                <option>1965</option>
                <option>1966</option>
                <option>1967</option>
                <option>1968</option>
                <option>1969</option>
                <option>1970</option>
                <option>1971</option>
                <option>1972</option>
                <option>1973</option>
                <option>1974</option>
                <option>1975</option>
                <option>1976</option>
                <option>1977</option>
                <option>1978</option>
                <option>1979</option>
                <option>1980</option>
                <option>1981</option>
                <option>1982</option>
                <option>1983</option>
                <option>1984</option>
                <option>1985</option>
                <option>1986</option>
                <option>1987</option>
                <option>1988</option>
                <option>1989</option>
                <option>1990</option>
                <option>1991</option>
                <option>1992</option>
                <option>1993</option>
                <option>1994</option>
                <option>1995</option>
                <option>1996</option>
                <option>1997</option>
                <option>1998</option>
                <option>1999</option>
                <option>2000</option>
                <option>2001</option>
                <option>2002</option>
                <option>2003</option>
                <option>2004</option>
                <option>2005</option>
                <option>2006</option>
                <option>2007</option>
                <option>2008</option>
                <option>2009</option>
                <option>2010</option>
            </select>               
        </p>
        <p><input type="button" value="Calculate" onclick="calcAge()" /></p>
        <h2>You have lived</h2>
        <p><input type="text" name="yearCalc" size="7" /> years</p>
        <p><input type="text" name="monthCalc" size="7" /> months</p>
        <p><input type="text" name="dayCalc" size="7" /> days</p>
        <p><input type="text" name="hourCalc" size="7" /> hours</p>
        <p><input type="text" name="minCalc" size="7" /> minutes</p>
    </form> 
<script>    
    function calcAge()
        {
        var d1 = new Date();
        var month = document.forms[0].month.value;
        var day = document.forms[0].day.value;
        var year = document.forms[0].year.value;
        var yearCalc = 2012 - year;
        var monthCalc = parseInt(month);
        var dayCalc = Math.abs(d1 - parseInt(day));
        var hourCalc = Math.round();
        var minCalc = Math.round();
        document.forms[0].yearCalc.value =  yearCalc.toLocaleString();
        document.forms[0].monthCalc.value =  monthCalc.toLocaleString();
        document.forms[0].dayCalc.value =  dayCalc.toLocaleString();
        document.forms[0].hourCalc.value =  hourCalc.toLocaleString();
        document.forms[0].minCalc.value =  minCalc.toLocaleString();
        }
</script>

4

1 回答 1

2

Date 对象 ( http://www.w3schools.com/jsref/jsref_obj_date.asp ) 预先打包了一些不错的函数,更值得注意的是 getTime() 函数,它返回 Date 对象自 1970 年以来的毫秒时间,基本相同作为 Unix 时间,除了单位(Unix 时间是秒)。要确定年龄,我们需要做的就是将用户的选择存储为 Date 对象,然后获取现在与用户输入的任何内容之间的差异。

var d1 = new Date();
d1.setMonth(document.forms[0].month.value);
d1.setDay(document.forms[0].day.value);
d1.setYear(document.forms[0].year.value);
var now = new Date();
var ageInMs = now.getTime() - d1.getTime();
var ageInYears = ageInMs / 86400000 / 365;

由于 getTime() 函数返回毫秒,我们需要将其转换为年。一天有 86,400,000 毫秒,一年有 365 天(大约)。

编辑:如果组合框实际上是字符串,您可能需要对组合框的值使用 parseInt() 。不过,这将是一个简单的改变——而不是

d1.setMonth(document.forms[0].month.value);

你会的

d1.setMonth(parseInt(document.forms[0].month.value, 10));
于 2013-03-29T20:15:07.293 回答