根据传入格式自动匹配替换,需要的人拿去用
使用方法:
timestampToYMD(time,'h:m');
timestampToYMD(time, 'M-D h:m');
// 时间戳转年月日
function timestampToYMD(unixtime, format) {
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
var returnArr = [];
var dateTime = new Date(parseInt(unixtime))
var year = dateTime.getFullYear();
var month = dateTime.getMonth() + 1;
var day = dateTime.getDate();
var hour = dateTime.getHours();
var minute = dateTime.getMinutes();
var second = dateTime.getSeconds();
returnArr.push(year);
returnArr.push(month<10?'0'+month:month);
returnArr.push(day < 10 ? '0' + day : day);
returnArr.push(hour < 10 ? '0' + hour : hour);
returnArr.push(minute < 10 ? '0' + minute : minute);
returnArr.push(second < 10 ? '0' + second : second);
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
return format;
}