ES6 Date对象详解

转载请注明预见才能遇见的博客:http://my.csdn.net/

原文地址:http://blog.csdn.net/pcaxb/article/details/56013991

ES6 Date对象详解

Date对象详解,创建对象

        let date = new Date();
1.基本方法调用

年月日,时分秒,毫秒的基本方法的调用,getYear 和getFullYear的区别

        //getYear获取的时间如果小于1900,那就要加上1900
        //比如 2017 ,getYear获取的时间就是117,加上1900就是2017
        var myYears = ( date.getYear() < 1900 ) ? ( 1900 + date.getYear() ) : date.getYear();

        //getFullYear获取的就是当前系统本地的年
        let year = date.getFullYear();

        //由于js的月份是从0开始的,所以月份加上1
        let month = date.getMonth()+1;

        //返回的是一个月中的某一天1-31
        let myDate = date.getDate();

        //返回的是一个星期中的某一天0-6,0是一个星期的第一天星期天
        let myDay = date.getDay();

        //获取24小时格式的小时
        let hours =  date.getHours();

        //分
        let minutes = date.getMinutes();

        //秒
        let seconds = date.getSeconds();

        //当前时间的毫秒(0-999),获取更精确的时间
        let milliseconds = date.getMilliseconds();
2.获取毫秒数的三种方式

        //获取1970到现在的毫秒数
        let time = date.getTime();

        //返回Date对象的原始值的毫秒数,
        //返回值和方法 Date.getTime 返回的值相等。
        let valueOfTime = date.valueOf();

        //parse() 方法可解析一个日期时间字符串,
        //并返回 1970/1/1 午夜距离该日期时间的毫秒数。
        //这个毫秒数是把当前毫秒变成000的毫秒数
        let parseTime = Date.parse(date.toString());

        //返回本地时间与格林威治标准时间 (GMT) 的分钟差,了解一下
        let timezoneOffset = date.getTimezoneOffset();
3.获取Date对象字符串和本地时间字符串

        //Date 对象,日期字符串
        console.log(date.toDateString());
        //Date 对象,时间字符串
        console.log(date.toTimeString());
        //Date 对象,日期+时间字符串
        console.log(date.toString());

        //日期字符串,根据本地时间格式
        console.log(date.toLocaleDateString());
        //时间字符串,根据本地时间格式
        console.log(date.toLocaleTimeString());
        //日期+时间字符串,根据本地时间格式
        console.log(date.toLocaleString());
4.当前毫秒数转化为时分秒

        //当前毫秒数转化为时分秒
        let timeToDate = new Date(1487590667000).toLocaleString();
        console.log(timeToDate);
        console.log(timeToDate.split("/").join('-'));
5.对Date的扩展,将 Date 转化为指定格式的String

        // 对Date的扩展,将 Date 转化为指定格式的String
        // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
        // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
        // 例子:
        // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
        // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
        Date.prototype.Format = function(fmt)
        { //author: meizz
            var o = {
                "M+" : this.getMonth()+1,                 //月份
                "d+" : this.getDate(),                    //日
                "h+" : this.getHours(),                   //小时
                "m+" : this.getMinutes(),                 //分
                "s+" : this.getSeconds(),                 //秒
                "q+" : Math.floor((this.getMonth()+3)/3), //季度
                "S"  : this.getMilliseconds()             //毫秒
            };
            if(/(y+)/.test(fmt))
                fmt=fmt.replace(RegExp.$1, (this.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;
        };

        console.log(date.Format('yyyy-MM-dd hh:mm:ss.S q'));
        console.log(date.Format('yyyy-M-d h:m:s.S q'));
W3C参考资料: 点击打开链接

ES6 Date对象详解

博客地址:http://blog.csdn.net/pcaxb/article/details/56013991




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值