原生js实现方法

就是将要转换的时间用new Date()重新生成一个时间,然后分别获取年月日时分秒(或者你需要的格式)重新拼接返回就行

function changeTime(time){
  let now = new Date(time),
      Y = now.getFullYear(),
      M = now.getMonth()+1,
      D= now.getDate(),
      h = now.getHours(),
      m = now.getMinutes(),
      s = now.getSeconds();
  M = M<10?'0'+M:M;
  D = D<10?'0'+D:D;
  h = h<10?'0'+h:h;
  m = m<10?'0'+m:m;
  s = s<10?'0'+s:s;
  return Y+'-'+M+'-'+D+' '+h+':'+m+':'+s;
}

效果

1.png


miomiomio
147 声望5 粉丝

人生昧履,砥砺而行