zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    使用 ServiceStack 构建跨平台 Web 服务
    .NET的微型Web框架 Nancy
    orcale复制表结构及其数据
    利用PL/SQL Developer工具导出数据到excel,导入excel数据到表
    PLSQL导入/导出数据方法
    基于Quqrtz.NET 做的任务调度管理工具
    Web监听器导图详解(转)
    GIT分支管理是一门艺术(转)
    我需要完全理解这部分代码才能确保它能够正常工作,如果由我来修复代码中的问题,我是不会这么写的,因此希望你也不要这么来写(转)
    不要学习代码,要学会思考(转)
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/12758708.html
Copyright © 2011-2022 走看看