js date-formatter
2016-05-12 11:51
603 查看
//这是一个用来格式化时间的扩展,只需要引用进来就可以,本案例是一个reactJS的写法 'use strict'; //this module is used to forma date //if you want to extend more,you can add your code here module.exports = function(module) { String.repeat = function(chr, count) { var str = ""; for(var i = 0; i < count; i++) { str += chr; } return str; }; String.prototype.padL = function(width, pad) { //not pad to left when width is 0 if (!width || width < 1) { return this; } //if pad string is empty, pad a empty string here; if (!pad) { pad=" "; } var length = width - this.length; if (length < 1) { return this.substr(0,width); } return (String.repeat(pad,length) + this).substr(0,width); }; String.prototype.padR = function(width, pad){ //not pad to right when width is 0 if (!width || width<1) { return this; } //if pad string is empty, pad a empty string here; if (!pad) { pad=" "; } var length = width - this.length; if (length < 1) { this.substr(0,width); } return (this + String.repeat(pad,length)).substr(0,width); }; Date.prototype.formatDate = function(format){ var date = this; //set a default formatter string if (!format) { format= "MM/dd/yyyy"; } var month = date.getMonth() + 1; var year = date.getFullYear(); format = format.replace("MM",month.toString().padL(2,"0")); if (format.indexOf("yyyy") > -1) { format = format.replace("yyyy",year.toString()); } else if (format.indexOf("yy") > -1) { format = format.replace("yy",year.toString().substr(2,2)); } format = format.replace("dd",date.getDate().toString().padL(2,"0")); var hours = date.getHours(); if (format.indexOf("t") > -1) { if (hours > 11) { format = format.replace("t","pm"); } else { format = format.replace("t","am"); } } if (format.indexOf("HH") > -1) { format = format.replace("HH",hours.toString().padL(2,"0")); } if (format.indexOf("hh") > -1) { if (hours > 12) hours - 12; if (hours == 0) hours = 12; format = format.replace("hh",hours.toString().padL(2,"0")); } if (format.indexOf("mm") > -1) { format = format.replace("mm",date.getMinutes().toString().padL(2,"0")); } if (format.indexOf("ss") > 4000 -1) { format = format.replace("ss",date.getSeconds().toString().padL(2,"0")); } return format; }; }; //使用 var newTimer = new Date(new Date().getTime()).formatDate('yyyy-MM-dd HH:mm');
优化版
module.exports = function (timer, fmt = 'YYYY-MM-DD HH:mm:ss') { var date = new Date(timer) var o = { 'M+': date.getMonth() + 1, 'D+': date.getDate(), 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, 'H+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds(), 'q+': Math.floor((date.getMonth() + 3) / 3), 'S': date.getMilliseconds() } var week = { '0': '\u65e5', '1': '\u4e00', '2': '\u4e8c', '3': '\u4e09', '4': '\u56db', '5': '\u4e94', '6': '\u516d' } if (/(Y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)) } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + '']) } 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 }
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Redux系列02:一个炒鸡简单的react+redux例子
- JavaScript 各种遍历方式详解
- call/apply/bind 的理解与实例分享