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

实现moment.js的实例详解

2024/10/17 4:43:44发布39次查看
项目中使用了时间日期的处理方法,只使用了部分方法,为了不在引入第三方的库(moment.js),这里自己封装了项目中使用到的方法。
要实现以下功能: 
new moment() // 返回当前的时间对象 new moment().unix() // 返回当前时间的秒数moment.unix(timestamp) // 传入秒数,返回传入秒数的时间对象 new moment().format('yyyy-mm-dd dd hh:mm:ss') // 返回值 2017-06-22 四 19:46:14moment.unix(1498132062000).format('yyyy-mm-dd dd hh:mm:ss') // 返回值 2017-06-22 四 19:46:14
一、基础代码:
1 class moment { 2     constructor(arg = new date().gettime()) { 3         this.date = new date(arg) 4     } 5 }
arg = new date().gettime() :这里使用解构对arg添加默认值
二、实现unix方法
 1 class moment {  2     constructor(arg = new date().gettime()) {  3         this.date = new date(arg)  4     }  5   6     // gettime()返回的是毫秒数,需要转成秒数并取整  7     unix() {  8         return math.round(this.date.gettime() / 1000)  9     } 10 }
unix方法:返回当前时间的秒数
gettime()方法返回的是毫秒数需要除1000取整处理
三、实现静态unix方法
1 class moment { 2     constructor(arg = new date().gettime()) { 3         this.date = new date(arg) 4     } 5  6     static unix(timestamp) { 7         return new moment(timestamp * 1000) 8     } 9 }
静态unix方法:参数timestamp 毫秒数  返回一个date对象
new date()只能接受毫秒的参数需要乘1000
四、实现format方法
 1 class moment {  2     constructor(arg = new date().gettime()) {  3         this.date = new date(arg)  4     }  5   6     format(formatstr) {  7         const date = this.date  8         const year = date.getfullyear()  9         const month = date.getmonth() + 1 10         const day = date.getdate() 11         const week = date.getday() 12         const hour = date.gethours() 13         const minute = date.getminutes() 14         const second = date.getseconds() 15  16         return formatstr.replace(/y{2,4}|m{1,2}|d{1,2}|d{1,4}|h{1,2}|m{1,2}|s{1,2}/g, (match) => { 17             switch (match) { 18             case 'yy': 19                 return string(year).slice(-2) 20             case 'yyy': 21             case 'yyyy': 22                 return string(year) 23             case 'm': 24                 return string(month) 25             case 'mm': 26                 return string(month).padstart(2, '0') 27             case 'd': 28                 return string(day) 29             case 'dd': 30                 return string(day).padstart(2, '0') 31             case 'd': 32                 return string(week) 33             case 'dd': 34                 return weeks[week] 35             case 'ddd': 36                 return '周' + weeks[week] 37             case 'dddd': 38                 return '星期' + weeks[week] 39             case 'h': 40                 return string(hour) 41             case 'hh': 42                 return string(hour).padstart(2, '0') 43             case 'm': 44                 return string(minute) 45             case 'mm': 46                 return string(minute).padstart(2, '0') 47             case 's': 48                 return string(second) 49             case 'ss': 50                 return string(second).padstart(2, '0') 51             default: 52                 return match 53             } 54         }) 55     } 56 }
format方法: 传入'yyy-mm-dd hh:mm:ss','yyy-mm-dd' 等,根据自己的传入要获取的格式(具体可参看moment.js的format的方法)
padstart()方法为月份或日期前补0
到这里我们就实现了unix,静态unix,format三个方法,完成代码可以去查看github,如果需要实现其它的日期时间处理方法可以给我留言,后面我会慢慢完善添加。
以上就是实现moment.js的实例详解的详细内容。
该用户其它信息

VIP推荐

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