Skip to content

Instantly share code, notes, and snippets.

@keenwon
Last active September 2, 2015 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keenwon/29ba1af7af95ca0e5931 to your computer and use it in GitHub Desktop.
Save keenwon/29ba1af7af95ca0e5931 to your computer and use it in GitHub Desktop.
时间格式化
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;
}
@keenwon
Copy link
Author

keenwon commented Jul 11, 2014

dateFormat('yyyy-MM-dd hh:mm:ss');
dateFormat(new Date(), 'yyyy-MM-dd hh:mm:ss');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment