js Date 日期格式化
2016-06-20 15:31
519 查看
Date.prototype.format = function (fmt) { 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; } // (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2002-05-04 10:03:05.423 // (new Date()).format("yyyy-M-d h:m:s.S") ==> 2002-5-4 10:3:5.18
相关文章推荐
- JS代码放在head和body中的区别分析
- JS 创建类的两种方法
- obj 转Json 保留空值字段
- JS学习记录(一)
- js取当前时间年月日
- JS实现的打字机效果完整实例
- 【JavaScript】BOM
- js全选 反选
- javascript创建对象
- js中读写cookie
- Javascript事件冒泡
- JSPatch 使用Demo
- 免费的天气接口
- Javascript学习笔记
- JavaScript toFixed() 方法
- HTML5代码可以和jsp混合在一起
- 36、JavaScript中两个DIV的碰撞检测
- Javascript中的arguments对象
- 浅析 JavaScript 链式调用
- JavaScript中获取HTML元素值的三种方法