时间戳转换

时间戳转换

首先要在js文件里面写个方法,如下:

// 时间戳转换
  formatDate(row, column) {
  // 13位时间戳
    let date = new Date(parseInt(row.CREATE_TIME));
    // 如果是10位数时间戳
    // let date = new Date(parseInt(row.CREATE_TIME)*1000);
    let Y = date.getFullYear() + '-';
    let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
    let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
    let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
    let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s;
  },

然后:在页面上对应位置设置

<el-table-column label="日期" prop="CREATE_TIME" :formatter="formatDate">el-table-column>

刷新页面就你能看到时间戳转换为"年月日 时分秒"的格式了
时间戳转换_第1张图片
如果,时间列的数据为空的话,会出现"*NAN NAN"*的字样,所以在转换之前要进行一下判断

// 时间戳转换
  formatDate(row, column) {
	  if (row.CREATE_TIME === 'null') {
	  	return false;
	  } else {
	  	// 13位时间戳
	    let date = new Date(parseInt(row.CREATE_TIME));
	    // 如果是10位数时间戳
	    // let date = new Date(parseInt(row.CREATE_TIME)*1000);
	    let Y = date.getFullYear() + '-';
	    let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
	    let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
	    let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
	    let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
	    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	    return Y + M + D + h + m + s;
	  }
  },

如果还有其他问题,欢迎大家给我留言~

你可能感兴趣的:(前端)