Last active
September 2, 2015 07:18
-
-
Save keenwon/29ba1af7af95ca0e5931 to your computer and use it in GitHub Desktop.
时间格式化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dateFormat(date, format) { | |
if(format === undefined){ | |
format = date; | |
date = new Date(); | |
} | |
var opt = { | |
"M+": date.getMonth() + 1, //月 | |
"d+": date.getDate(), //日 | |
"h+": date.getHours(), //時 | |
"m+": date.getMinutes(), //分 | |
"s+": date.getSeconds(), //秒 | |
"q+": Math.floor((date.getMonth() + 3) / 3), //季 | |
"S": date.getMilliseconds() //毫秒 | |
}; | |
if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); | |
for (var o in opt) { | |
if (new RegExp("(" + o + ")").test(format)) format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (opt[o]) : (("00" + opt[o]).substr(("" + opt[o]).length))); | |
} | |
return format; | |
} |
Author
keenwon
commented
Jul 11, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment