实现简易版的moment.js
github源码地址: www.baidu.com
作者: 易怜白
项目中使用了时间日期的处理方法,只使用了部分方法,为了不在引入第三方的库(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:14 Moment.unix(1498132062000).format('YYYY-MM-DD dd HH:mm:ss') // 返回值 2017-06-22 四 19:46:14
一、基础代码:
class Moment { constructor(arg = new Date().getTime()) { this.date = new Date(arg) } }
arg = new Date().getTime() :这里使用解构对arg添加默认值
二、实现unix方法
class Moment { constructor(arg = new Date().getTime()) { this.date = new Date(arg) } // getTime()返回的是毫秒数,需要转成秒数并取整 unix() { return Math.round(this.date.getTime() / 1000) } }
unix方法:返回当前时间的秒数
三、实现静态unix方法
class Moment { constructor(arg = new Date().getTime()) { this.date = new Date(arg) } static unix(timestamp) { return new Moment(timestamp * 1000) } }
静态unix方法:参数timestamp 毫秒数 返回一个Date对象
四、实现format方法
class Moment { constructor(arg = new Date().getTime()) { this.date = new Date(arg) } format(formatStr) { const date = this.date const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const week = date.getDay() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() 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) => { switch (match) { case 'YY': return String(year).slice(-2) case 'YYY': case 'YYYY': return String(year) case 'M': return String(month) case 'MM': return String(month).padStart(2, '0') case 'D': return String(day) case 'DD': return String(day).padStart(2, '0') case 'd': return String(week) case 'dd': return weeks[week] case 'ddd': return '周' + weeks[week] case 'dddd': return '星期' + weeks[week] case 'H': return String(hour) case 'HH': return String(hour).padStart(2, '0') case 'm': return String(minute) case 'mm': return String(minute).padStart(2, '0') case 's': return String(second) case 'ss': return String(second).padStart(2, '0') default: return match } }) } }
实现简易版的moment.js的更多相关文章
- 来,我们手写一个简易版的mock.js吧(模拟fetch && Ajax请求)
预期的mock的使用方式 首先我们从使用的角度出发,思考编码过程 M1. 通过配置文件配置url和response M2. 自动检测环境为开发环境时启动Mock.js M3. mock代码能直接覆盖g ...
- 一个极其简易版的vue.js实现
前言 之前项目中一直在用vue,也边做边学摸滚打爬了近一年.对一些基础原理性的东西有过了解,但是不深入,例如面试经常问的vue的响应式原理,可能大多数人都能答出来Object.defineProper ...
- 一个简易版的Angular js 三层 示例
var myApp = angular.module('produceline', []); myApp.factory('ajax', ["$http", "$q&qu ...
- 实现简易版德州扑克|学习麻瓜编程以项目为导向入门前端 HTML+CSS+JS
实现简易版德州扑克 1.先上达到网页效果图(简易版德州扑克) 网页分为发牌区和牌池,上面为发牌区,下面是牌池区 2. 代码实现 2.1 HTML和JS代码 ` <link rel="s ...
- 使用 js 和 Beacon API 实现一个简易版的前端埋点监控 npm 包
使用 js 和 Beacon API 实现一个简易版的前端埋点监控 npm 包 前端监控,埋点,数据收集,性能监控 Beacon API https://caniuse.com/beacon 优点,请 ...
- 使用 js 实现一个简易版的模版引擎
使用 js 实现一个简易版的模版引擎 regex (function test() { this.str = str; })( window.Test = ...; format() { let ar ...
- 使用 js 实现一个简易版的 drag & drop 库
使用 js 实现一个简易版的 drag & drop 库 具有挑战性的前端面试题 H5 DnD js refs https://www.infoq.cn/article/0NUjpxGrqRX ...
- 使用 js 实现一个简易版的动画库
使用 js 实现一个简易版的动画库 具有挑战性的前端面试题 animation css refs https://www.infoq.cn/article/0NUjpxGrqRX6Ss01BLLE x ...
- 使用 js 实现一个简易版的 GIPHY 动图搜索 web 应用程序
使用 js 实现一个简易版的 GIPHY 动图搜索 web 应用程序 具有挑战性的前端面试题 API JAMstack refs https://www.infoq.cn/article/0NUjpx ...
随机推荐
- hdu2444二分图最大匹配+判断二分图
There are a group of students. Some of them may know each other, while others don't. For example, A ...
- mysql for windows(服务器)上的配置安装--实例
mysql for windows(服务器)上的配置安装 **** 下载 官网网址:https://www.mysql.com/downloads/ 选择左上角Community 再选择MySQL C ...
- abstract、virtual、sealed
abstract与virtual前必有public 1.abstract,抽象 1)只要使用到了abstract定义了方法,那么类就一定得用abstract定义,public abstract,只有抽 ...
- FME中通过HTMLExtractor向HTML要数据
如何不断扩充数据中心的数据规模,提升数据挖掘的价值,这是我们思考的问题,数据一方面来自于内部生产,一部分数据可以来自于互联网,互联网上的数据体量庞大,形态多样,之前blog里很多FMEer已经提出了方 ...
- zoj3204 Connect them 最小生成树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目就是简单的最小生成树的模板的应用,不过最小生成树可能不唯一 ...
- 适配器模式(Adpater Pattern)
适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的用途 用电器做例子,笔记本电脑的插头一般都是三相的,即除了阳极.阴极 ...
- 一步步学习EF Core(2.事务与日志)
前言 上节我们留了一个问题,为什么EF Core中,我们加载班级,数据并不会出来 其实答案很简单,~ 因为在EF Core1.1.2 中我们在EF6.0+中用到的的延迟加载功能并没有被加入,不过在EF ...
- idea 控制台输出 中文乱码 解决方法
使用intellij idea 14.1时,console 会输出中文乱码.下面分两种情况解决这种问题:一种是maven构建项目.一种是tomcat(不以maven构建)构建项目. 1.tomcat输 ...
- 集群/分布式环境下5种session处理策略
转载自:http://blog.csdn.net/u010028869/article/details/50773174?ref=myread 前言 在搭建完集群环境后,不得不考虑的一个问题就是用户访 ...
- Word Ladder 2015年6月3日
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...