js格式化时间
2015-07-29 13:10
465 查看
/* * date 需要格式化的时间 * farmatStr 需要格式化的样式 如:yyyy-MM-dd */ function dateFormat(date,formatStr){ // 转换成Date类型 var date = new Date(date); var opt = { 'yyyy': date.getFullYear(), 'MM': addZero(date.getMonth() + 1), 'M': date.getMonth() + 1, 'dd': addZero(date.getDate()), 'd': date.getDate(), 'hh': addZero(date.getHours()), 'h': date.getHours(), 'mm': addZero(date.getMinutes()), 'm': date.getMinutes(), 'ss': addZero(date.getSeconds()), 's': date.getSeconds() }; // 如果是个位数则前面添加0 function addZero(value){ return value < 10 ? '0'+value : value; } // 遍历替换 for(var k in opt){ formatStr = formatStr.replace(k,opt[k]); } return formatStr; } 如何使用: dateFormat('2014-2-3','yyyy-MM-dd'); 得到 "2014-02-03" dateFormat(new Date(),'yyyy-MM-dd'); 得到当前时间
相关文章推荐
- javascript一秒刷新一次显示时间
- JSP中传递参数的几种方法的简介
- js this指向问题
- js Unicode编码转换
- 详解JavaScript的Polymer框架中的通知交互
- JavaScript的Polymer框架中dom-repeat与VM的相关操作
- 深入掌握JMS(一):JSM基础
- javascript个人代码备份2015-07-29
- jsp 基础知识之指令元素
- js实现图片轮播
- 浅谈JavaScript的Polymer框架中的事件绑定
- 探讨JavaScript中的Rest参数和参数默认值
- JS的框架Polymer中的dom-if和is属性使用说明
- 跨域方案:jsonp的使用
- Json 和 Jsonlib 的使用
- jsp/servlet中的编码问题
- 高性能JavaScript 重排与重绘
- 【JSP基础】JSP实现登录注册界面
- js数组去重的方法汇总
- JS中prototype详解