javascript将后台取得的时间戳转化为时间格式

  • baagee 发布于 2016-09-13 23:03:39
  • 分类:JavaScript
  • 969 人围观
  • 2 人喜欢


function getDateTime(timestamp) {
  /*重写toLocaleString方法*/
  Date.prototype.toLocaleString = function() {
    Y = this.getFullYear() + '-';
    M = (this.getMonth()+1 < 10 ? '0'+(this.getMonth()+1) : this.getMonth()+1) + '-';
    D = (this.getDate()<10?'0' +this.getDate():this.getDate())+ ' ';
    h = (this.getHours()<10?'0'+this.getHours():this.getHours()) + ':';
    m = (this.getMinutes()<10?'0'+this.getMinutes():this.getMinutes()) + ':';
    s = (this.getSeconds()<10?'0'+this.getSeconds():this.getSeconds());
    return Y+M+D+h+m+s;
  };
  return new Date(timestamp*1000).toLocaleString();
}

或者下面的方法:

function getDateTime(timestamp) {
  var date = new Date(timestamp*1000);
  Y = date.getFullYear() + '-';
  M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  D = (date.getDate()<10?'0' +date.getDate():date.getDate())+ ' ';
  h = (date.getHours()<10?'0'+date.getHours():date.getHours()) + ':';
  m = (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()) + ':';
  s = (date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds());
  return Y+M+D+h+m+s;
}


评论

点击图片切换
还没有评论,快来抢沙发吧!