Last active
January 25, 2016 05:53
-
-
Save piaoger/f66f0bade7958e9140e1 to your computer and use it in GitHub Desktop.
format date utc
This file contains hidden or 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
// borrowed from http://www.cnblogs.com/duanhuajian/p/4485106.html | |
// change: use utc time instead | |
function formatDate (date, fmt) { | |
var o = { | |
"M+" : date.getUTCMonth()+1, | |
"d+" : date.getUTCDate(), | |
"h+" : date.getUTCHours()%12 == 0 ? 12 : date.getUTCHours()%12, | |
"H+" : date.getUTCHours(), | |
"m+" : date.getUTCMinutes(), | |
"s+" : date.getUTCSeconds(), | |
"q+" : Math.floor((date.getUTCMonth()+3)/3), | |
"S" : date.getUTCMilliseconds() | |
}; | |
var week = { | |
"0" : "/u65e5", | |
"1" : "/u4e00", | |
"2" : "/u4e8c", | |
"3" : "/u4e09", | |
"4" : "/u56db", | |
"5" : "/u4e94", | |
"6" : "/u516d" | |
}; | |
if(/(y+)/.test(fmt)){ | |
fmt=fmt.replace(RegExp.$1, (date.getUTCFullYear()+"").substr(4 - RegExp.$1.length)); | |
} | |
if(/(E+)/.test(fmt)){ | |
fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[date.getUTCDay()+""]); | |
} | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment