ES6中date对象的方法

转载自

        let date = new Date();

创建Date对象

一,基本方法调用

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

//getYear 获取的时间如果小于1900,需要加上1900
//例如 2019获取的就是119 加上1900才是2019
let myYears = ( date.getYear() < 1900 ) ?(1900 + date.getYear()):dateYear();

//getFullYear获取的是当前系统时间
let year = date.getFullYear();

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

//返回的是一个月中的某一天
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 millisconds = date.getMilliseconds();

二,获取毫秒数的三种方式

//获取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();

三,获取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());

四,当前毫秒数转化为时分秒

        //当前毫秒数转化为时分秒
        let timeToDate = new Date(1487590667000).toLocaleString();
        console.log(timeToDate);
        console.log(timeToDate.split("/").join('-'));

五,对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'));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值