日期:2014-05-16  浏览次数:20426 次

JS由日期算出年龄
如:已经知道 "2002-10-12" 通过时间戳怎么算出年龄啊。。谢谢~~

------解决方案--------------------
HTML code
    <form>
        <label for="birthday">生日:</label>
        <input id="birthday" name="birthday" type="text" value="2002-10-12" />
        <input id="button" type="button" value="提交" />
        <div>
            <span>年龄:</span>
            <span id="result"></span>
        </div>
    </form>

    <script>
        document.getElementById("button").onclick = function(){
            var now, birthday, age;
            now = new Date();
            birthday = new Date(document.getElementById("birthday").value);
            age = new Date(now.getTime() - birthday.getTime()).getFullYear();
            document.getElementById("result").innerHTML = age - 1970;
        }
    </script>