js日期格式转换

 
//毫数转格式 【yyyy-MM-dd】 
 var format = function(time, format){ 
var t = new Date(time); 
var tf = function(i){return (i < 10 ? '0' : '') + i}; 
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){ 
switch(a){ 
case 'yyyy': 
return tf(t.getFullYear()); 
break; 
case 'MM': 
return tf(t.getMonth() + 1); 
break; 
case 'mm': 
return tf(t.getMinutes()); 
break; 
case 'dd': 
return tf(t.getDate()); 
break; 
case 'HH': 
return tf(t.getHours()); 
break; 
case 'ss': 
return tf(t.getSeconds()); 
break; 
}; 
}); 
}; 
 
alert(format(1396178344662, 'yyyy-MM-dd HH:mm:ss')); 

/**
* 时间转换,yyyy-MM-dd 转毫秒数
*/
function getDate(strDate) {
var date = eval('new Date('
+ strDate.replace(/\d+(?=-[^-]+$)/, function(a) {
return parseInt(a, 10) - 1;
}).match(/\d+/g) + ')');
return date;
}

posted on 2016-06-28 15:10  邵海雄  阅读(13161)  评论(0编辑  收藏  举报

导航