有时使用一些公开api会碰到UTC时间格式,而将UTC时间格式转换成本地时间格式可以采用以下js办法
UTCformat (utc) {
var date = new Date(utc),
year = date.getFullYear(),
month = date.getMonth()+1 > 9 ? date.getMonth()+1 : '0' + parseInt(date.getMonth()+1),
day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate(),
hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours(),
minutes = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(),
seconds = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();
var res = year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' +seconds;//将上述拆分的数据整合,连接符可以自己决定,如ios无法识别2xxx-xx-xx格式可将'-'改为'/'
return res;
}