您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何理解ES6 Date对象以及对象的创建

2024/5/2 18:36:41发布20次查看
本篇文章给大家带来的内容是关于如何理解es6 date对象以及对象的创建,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
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'))
以上就是如何理解es6 date对象以及对象的创建的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product