js时间播报

2017-09-25  本文已影响0人  Arno_z

js实现中央播报台时间播报。例如:今天是北京时间:二〇一五年三月十二日 星期三 五时三十八分

<script type="text/javascript">
    function myTime(){
        var now = new Date();
        var year = (now.getFullYear()).toString();
        var month = now.getMonth() + 1;
        var day = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = (now.getSeconds()).toString();
        if(second.length < 2){
            second = "0" + second;
        }
        var weekDay = now.getDay();
        var map = {
            0: "〇",
            1: "一",
            2: "二",
            3: "三",
            4: "四",
            5: "五",
            6: "六",
            7: "七",
            8: "八",
            9: "九"
        };
        var weekMap = {
            0: "日",
            1: "一",
            2: "二",
            3: "三",
            4: "四",
            5: "五",
            6: "六"
        }
        var res = "今天是" + map[parseInt(year[0])] + map[parseInt(year[1])] + map[parseInt(year[2])]+map[parseInt(year[3])] + "年";
        if(month >= 10){
            if(month == 10){
                res += "十月";
            }else{
                res += "十";
                res += map[month%10] + "月";
            }
        }else{
            res += map[month] + "月";
        }
        if(day > 10){
            if(day == 10){
                res += "十日";
            }else if(day < 20){
                res += "十";
                res += map[day%10] + "日";
            }else{
                res += map[parseInt(day/10)];
                res += "十";
                res += map[day%10] + "日";
            }
        }
        if(hour > 10){
            if(hour == 10){
                res += "十点";
            }else if(hour < 20){
                res += "十";
                res += map[hour%10] + "点";
            }else{
                res += map[parseInt(hour/10)];
                res += "十";
                res += map[hour%10] + "点";
            }
        }
        if(minute > 10){
            if(minute == 10){
                res += "十分";
            }else if(minute < 20){
                res += "十";
                res += map[minute%10] + "分";
            }else{
                res += map[parseInt(minute/10)];
                res += "十";
                res += map[minute%10] + "分";
            }
        }
        res += "星期" + weekMap[weekDay];
        console.log(res);
    }
    myTime();
</script>
上一篇 下一篇

猜你喜欢

热点阅读