js时间戳转化成日期格式

前言:

时间戳:1540460693,需转化成时间格式:2018/10/25

 

// 时间戳转换日期格式
function timeFormat(nS) {
  let date = new Date(parseInt(nS) * 1000) // 时间戳为10位需乘1000,为13位则不用

  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,以下仅做示例
  return Y + '-' + M + '-' + D // yyyy-mm-dd
  return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + 's' // yyyy-mm-dd hh:mm:ss

  return Y + '/' + M + '/' + D // yyyy/mm/dd
  return Y + '/' + M + '/' + D + ' ' + h + ':' + m + ':' + 's' // yyyy/mm/dd hh:mm:ss

}

 

posted on 2019-01-22 16:11  白小鸽  阅读(1506)  评论(0编辑  收藏  举报