前端常用代码片段:时间格式化
目 录
前端常用代码片段:时间格式化
new Date().format('yyyy-MM-dd'); // "2017-02-18"
new Date().format('yyyy-MM-dd hh-mm-ss'); //
"2017-02-18 04-41-08"
new Date().format('yyyy-MM-dd hh/mm/ss'); //
"2017-02-18 04/41/18"
new Date().format('yyyy-MM-dd HH/mm/ss'); //
"2017-02-18 16/42/30"
new Date().format('yyyy-MM-dd E HH/mm/ss');
//"2017-02-18 六 16/51/16"
new Date().format('yyyy-MM-dd EE HH/mm/ss'); //
"2017-02-18 周六 16/51/30"
new Date().format('yyyy-MM-dd EEE HH/mm/ss'); //
"2017-02-18 星期六 16/51/77"
示例源码:
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //12小时
"H+": this.getHours(), //24小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
var week = {
"0": "日",
"1": "一",
"2": "二",
"3": "三",
"4": "四",
"5": "五",
"6": "六"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "星期" : "周") : "") + week[this.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;
}
new Date().format('yyyy-MM-dd'); // "2017-02-18"
new Date().format('yyyy-MM-dd hh-mm-ss'); //
"2017-02-18 04-41-08"
new Date().format('yyyy-MM-dd hh/mm/ss'); //
"2017-02-18 04/41/18"
new Date().format('yyyy-MM-dd HH/mm/ss'); //
"2017-02-18 16/42/30"
new Date().format('yyyy-MM-dd E HH/mm/ss');
//"2017-02-18 六 16/51/16"
new Date().format('yyyy-MM-dd EE HH/mm/ss'); //
"2017-02-18 周六 16/51/30"
new Date().format('yyyy-MM-dd EEE HH/mm/ss'); //
"2017-02-18 星期六 16/51/77"
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //12小时
"H+": this.getHours(), //24小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
var week = {
"0": "日",
"1": "一",
"2": "二",
"3": "三",
"4": "四",
"5": "五",
"6": "六"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "星期" : "周") : "") + week[this.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;
}
jQuery制作橙色的新浪顶部下拉菜单,鼠标悬停文字导航栏展开二级下拉菜单代码。 相关推荐: wiper响应式全屏banner图片滚动轮播代码 jQuery基于swiper.js库制作响应式宽屏的banner图片文字滚动轮播,支持触屏滑动切换。适用于响应式网站…
关注微信公众号『OOINK』
第一时间了解最新资源动态关注OO.INK不迷路~
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 下载资源需要解压密码,解压密码是什么?
1、本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
OO.INK资源网 » 前端常用代码片段:时间格式化
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
OO.INK资源网 » 前端常用代码片段:时间格式化