js格式化时间的方法
2017-10-27 16:49
330 查看
js格式化时间的方法:
使用示例:
效果如下图:
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function (fmt) { //author: meizz 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; }
使用示例:
$(document).ready(function(){ //给时间控件加上样式 $("#tb").find("input[name='cruMon_begin']").attr("class","Wdate").click(function(){WdatePicker({dateFmt:'yyyy-MM'});}); $("#tb").find("input[name='cruMon_end']").attr("class","Wdate").click(function(){WdatePicker({dateFmt:'yyyy-MM'});}); $("#tb").find("input[name='cruMon_begin']").attr("value",new Date().Format("yyyy-MM")); $("#tb").find("input[name='cruMon_end']").attr("value",new Date().Format("yyyy-MM")); });
效果如下图:
![](https://images2017.cnblogs.com/blog/993337/201710/993337-20171027164844648-1443776522.png)
相关文章推荐
- js获取日期、时间、日期常用方法、时间计算并格式化和实现电子表
- JS获取当前日期和时间的方法,并按照YYYY-MM-DD格式化
- js常用方法_格式化金额_比较时间对象_秒转时分秒
- JS扩展方法 包括时间的格式化
- js实现格式化金额,字符,时间的方法
- js 格式化显示时间方法
- js格式化时间类的方法
- JS获取当前日期和时间的方法,并按照YYYY-MM-DD格式化
- js,jquery获取和格式化时间方法总结
- js格式化时间的方法
- 【工作笔记】js常用方法--格式化时间、格式化金额
- js格式化时间的方法
- js时间戳格式化成日期格式的多种方法
- js时间格式化方法
- js封装方法用于格式化时间
- js中时间格式化的几种方法
- js 中时间格式化的几种方法
- js格式化日期时间的工具方法