Skip to content

Instantly share code, notes, and snippets.

@callblueday
Last active December 20, 2015 19:18
Show Gist options
  • Save callblueday/6181855 to your computer and use it in GitHub Desktop.
Save callblueday/6181855 to your computer and use it in GitHub Desktop.
格式化日期,将毫秒转化成字符串的快捷函数
/**
* Format Date
* usage:
* date2str(new Date(seconds*1000),"MM-dd hh:mm");
*/
date2str = function(x,y) {
var z = {M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()};
y = y.replace(/(M+|d+|h+|m+|s+)/g,function(v) {
return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-2)
});
return y.replace(/(y+)/g,function(v) {return x.getFullYear().toString().slice(-v.length)});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment