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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM