日期和时间对象

JavaScript日期时间对象与格式化
这篇博客介绍了JavaScript中的日期时间对象Date的使用,包括如何创建及获取年、月、日、小时等信息。还展示了如何通过函数进行日期时间的格式化,提供了`dateFtt`函数用于自定义日期时间的显示格式。最后,通过`formateDate`函数展示了多种日期时间格式化的实例。
<!-- 
        日期时间对象 Date  
          => 提供的内置构造函数,专门用来获取时间的
          
          => 创建日期时间对象
              var timeObj = new Date()
              创建指定时间的日期时间对象
               var time = new Date('2020-3-10 15:10:35')
               var time = new Date(2020,3,10, 15,10,35')

          => 方法


// var date1 = new Date() //当前时间
        // console.log(date1); // Thu Dec 02 2021 15:10:35 GMT+0800 (中国标准时间)

        // var time1 = new Date('2020-3-10 15:10:35') // 创建指定时间的日期时间对象
        // console.log(time1);
        var date1 = new Date()
        console.log(date1)
        // var time1 = new Date('2020-12-02 17:45:40')
        // console.log(time1)


var time = new Date() //当前时间
        
        // console.log('time.getFullYear()  :', time.getFullYear())
        // console.log('time.getMonth()  :', time.getMonth()+1)
        // console.log('time.getDate()  :', time.getDate())

        // console.log('time.getHours()  :', time.getHours())
        // console.log('time.getMinutes()  :', time.getMinutes())
        // console.log('time.getSeconds()  :', time.getSeconds())

        // console.log('time.getDay()  :', time.getDay())
        // console.log('time.getTime()  :', time.getTime())

优化

// function gettime(){
            //     var time = new Date()
            //     var year = time.getFullYear()
            //     var month = time.getMonth()
            //     var date = time.getDate()
            //     var hours = time.getHours()
            //     var minues = time.getMinutes()
            //     var seconds = time.getSeconds()

            //     console.log (`${year}-${month}-${date} ${hours}-${minues}-${seconds}`)
                
            // }
            
            //  gettime()


/*
          格式化日期时间
        */
        // function formateCurrentDate() {
        //     var time = new Date() //当前日期时间对象
        //     var year = time.getFullYear() //年
        //     var month = time.getMonth() //月
        //     var date = time.getDate() //日期
        //     var hours = time.getHours() //小时
        //     var minues = time.getMinutes() //分钟
        //     var seconds = time.getSeconds() //秒

        //     //2021-12-02 15:24:49
        //     var dateStr = `${year}-${month+1}-${date} ${hours}:${minues}:${seconds}`
        //     return dateStr
        // }

        // var time = formateCurrentDate()
        // console.log(time);







function dateFtt(fmt, date) {
            var o = {
                "M+": date.getMonth() + 1, //月份
                "d+": date.getDate(), //日
                "h+": date.getHours(), //小时
                "m+": date.getMinutes(), //分
                "s+": date.getSeconds(), //秒
                "q+": Math.floor((date.getMonth() + 3) / 3), //季度
                S: date.getMilliseconds(), //毫秒
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(
                    RegExp.$1,
                    (date.getFullYear() + "").substr(4 - RegExp.$1.length)
                );
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(
                        RegExp.$1,
                        RegExp.$1.length == 1 ?
                        o[k] :
                        ("00" + o[k]).substr(("" + o[k]).length)
                    );
            return fmt;
        }

        //创建时间格式化显示
        function formateDate() {
            var crtTime = new Date(); //当前时间
            return dateFtt("yyyy-MM-dd hh:mm:ss", crtTime);
            return dateFtt("yyyy年MM月dd日 hh:mm:ss", crtTime);  
            return dateFtt("yyyy/MM/dd hh:mm:ss", crtTime);  
            return dateFtt("yyyy年MM月dd日", crtTime);  

        }


        var date1 = formateDate()
        console.log(date1);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值