获得某某年某月某日某时分秒的时间,头部添0用到padStart()

 <script>
        /*  padStart(2,'0');头部补0,2表示字符串不足2位时,自动添0;
     padEnd(字符串的位数,'添加的字符串');尾部补充;
     今天是 2020 年 7 月 7 号, 星期二 , 下午 16:38:00 */
        var date = new Date();
        var year = date.getFullYear();
        var month = month = (date.getMonth() + 1).toString().padStart(2, '0');
        var day = date.getDate().toString().padStart(2, '0');
        var week = date.getDay();
        switch (week) {
            case 0:
                wek = '星期天';
                break;
            case 1:
                wek = '星期一';
                break;
            case 2:
                wek = '星期二';
                break;
            case 3:
                wek = '星期三';
                break;
            case 4:
                wek = '星期四';
                break;
            case 5:
                wek = '星期五';
                break;
            case 6:
                wek = '星期六';
                break;
        }
        var hour = date.getHours().toString().padStart(2, '0');
        var minute = date.getMinutes().toString().padStart(2, '0');
        var second = date.getSeconds().toString().padStart(2, '0');
        if (hour < 12) {
            var Time = '今天是' + year + '年' + month + '月' + day + '号,' + wek + ',早上' + hour + ':' + minute + ':' + second;
            console.log(Time);

        } else if (hour == 12) {
            var Time = '今天是' + year + '年' + month + '月' + day + '号,' + wek + ',中午' + hour + ':' + minute + ':' + second;
            console.log(Time);
        } else {
            var Time = '今天是' + year + '年' + month + '月' + day + '号,' + wek + ',下午' + hour + ':' + minute + ':' + second;
            console.log(Time);
        }
        //06:23:08;


    </script>

你可能感兴趣的:(js)