10.JavaScript距離生日還有多少天、根據出生年月日計算年齡、打印當前月份每天的星期


1.距離生日還有多少天:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //給出生日的月份和日期,計算還有多少天過生日
        function getDaysToBirthday(month, day) {
            var nowTime = new Date();
            var thisYear = nowTime.getFullYear();
            //今年的生日
            var birthday = new Date(thisYear, month - 1, day);

            //今年生日已過,則計算距離明年生日的天數
            if (birthday < nowTime) {
                birthday.setFullYear(nowTime.getFullYear() + 1);
            }
            var timeDec = birthday - nowTime;
            var days = timeDec / (24 * 60 * 60 * 1000);
            return Math.ceil(days);
        }
        getDaysToBirthday(11, 2);
    </script>
</body>

</html>
index.html

 

2.根據出生年月日,計算年齡:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //是否閏年
        function isLeap(year){
            return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
        }

        //根據出生年月日,計算年齡
        function getAge(year, month, day) {
            //得到當前日期
            var nowTime = new Date();
            
            var age = nowTime.getFullYear() - year;//粗略計算出年齡

            //若生日是2月29日,且今年不是閏年,則今年生日日期按28日計算
            if(month === 2 && day === 29 && !isLeap(nowTime.getFullYear())){
                day = 28;
            }

            //得到今年的生日日期
            var nowBirthday = new Date(nowTime.getFullYear(), month - 1, day);
            console.log(nowBirthday, nowTime);
            if(nowBirthday > nowTime){//若今年的生日沒有過,則減一歲
                age--;
            }

            return age;
        }

        console.log(getAge(2000, 2, 29));
    </script>
</body>

</html>
index.html

 

3.打印當前月份每天的星期:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //獲取星期的漢字形式
        function getDayOfWeed(i){
            var arr = ["一","二","三","四","五","六","日"];
            return arr[i];
        }

        //打印當前月每一天的星期
        function printDay(){
            var nowTime = new Date();
            var year = nowTime.getFullYear();
            var month = nowTime.getMonth() + 1;
            //得到當前月的總天數
            var days = new Date(year, month, 0).getDate();//后一個月份減一天,得到當前月的最后一天的日期

            for(var i = 1; i <= days; i++){
                console.log(`${year}年${month}月${i}日:星期${getDayOfWeed(new Date(year, month - 1, i).getDay())}`);
            }
        }

        printDay();
    </script>
</body>

</html>
index.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM