JS格式化日字符串
2016-04-07 17:43
447 查看
要格式的日期字符串为“2016-11-13 12:11:10”
Date.prototype.format = function(format) {
var o = {
"M+" :this.getMonth() + 1, // month
"d+" :this.getDate(), // day
"h+" :this.getHours(), // hour
"m+" :this.getMinutes(), // minute
"s+" :this.getSeconds(), // second
"q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
"S" :this.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for ( var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
Date.prototype.format = function(format) {
var o = {
"M+" :this.getMonth() + 1, // month
"d+" :this.getDate(), // day
"h+" :this.getHours(), // hour
"m+" :this.getMinutes(), // minute
"s+" :this.getSeconds(), // second
"q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
"S" :this.getMilliseconds()
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for ( var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
var date = new Date(Date.parse(dateStr.replace(/-/g, "/"))); date.format("yyyy-MM-dd hh:mm:ss");
相关文章推荐
- zepto.js的事件处理
- Angular.js 学习笔记
- JavaScript 左右上下自动晃动,自动移动。
- 预防阻塞的方法
- OC与JS的简单交互
- Extjs问题——windows包容多组件
- js 小键盘需求
- jsp页面显示不全问题
- javascript高级选择器querySelector和querySelectorAll全面解析
- json文件的相关细节
- login.jsp
- 关于cookie的初识和运用(js和jq)
- 纯js实现瀑布流布局及ajax动态新增数据
- javaScript获取文档中所有元素节点的个数
- jsp页面 链接中文文件名 找不到页面
- 【笔记】 《js权威指南》- 第15章 脚本化文档 - 15.9 HTML表单
- 原生JavaScript实现Ajax的方法
- JavaScript数据推送Comet技术详解
- jsoncpp库
- js实现商品抛物线加入购物车特效