var timestampToTime = function (timestamp ,format) {
var format_Arr = ['Y', 'M', 'D', 'h', 'm', 's'];
timestamp = timestamp.toString().length === 13 ? Number(timestamp) : timestamp*1000;
var data = new Date(timestamp);
var Y = zerofill(data.getFullYear()),
M = zerofill(data.getMonth()+1),
D = zerofill(data.getDate()),
h = zerofill(data.getHours()),
m = zerofill(data.getMinutes()),
s = zerofill(data.getMilliseconds());
var time_Arr = [Y, M, D, h, m, s];
time_Arr.forEach(function (value, index) {
format = format.replace(format_Arr[index], value);
});
return format;
function zerofill (time) {
var tiem_str = time.toString();
return tiem_str[1] ? tiem_str : '0' + tiem_str;
}
};
参考文献:www.jianshu.com/p/5d380b68c…