js時間戳轉換年月日
阿新 • • 發佈:2018-12-19
直接上程式碼: getDate方法接受兩個引數,引數一為需要轉換的格式,如yyyy-mm-dd,或yyyy/mm/dd,其他格式可以根據需要自主新增;引數2為時間戳
//轉換年月日方法
function getDate(format,str){
var oDate = new Date(str),
oYear = oDate.getFullYear(),
oMonth = oDate.getMonth()+1,
oDay = oDate.getDate(),
oHour = oDate.getHours(),
oMin = oDate. getMinutes(),
oSec = oDate.getSeconds(),
if(format == 'yyyy-mm-dd'){
oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSec);//最後拼接時間
}else if(format == 'yyyy/mm/dd'){
oTime = oYear +'/'+ getzf(oMonth) +'/'+ getzf(oDay) +' '+ getzf (oHour) +':'+ getzf(oMin) +':'+getzf(oSec);//最後拼接時間
}
return oTime;
};
//補0操作
function getzf(num){
if(parseInt(num) < 10){
num = '0'+num;
}
return num;
}