js-时间戳日期格式互转

45次阅读

共计 1051 个字符,预计需要花费 3 分钟才能阅读完成。

时间转时间戳

  1. 当前时间转换(毫秒)
new Date().getTime()
  1. 指定时间转换(毫秒)
var timeDate = "2019-05-09 14:50:48";
new Date(timeDate).getTime()

时间戳转化为日期

时间转换 – 格式 1 时 1 分 11 秒

dateConvert(value){
    let hour,min,sec,createDate=value
    hour=parseInt(createDate/60/60)
    min=parseInt((createDate-hour*3600)/60)
    sec=parseInt(createDate-(hour*3600+min*60))
    if(hour>0){if(min>0){if(sec>0){createDate=hour+'时'+min+'分'+sec+'秒'} else{createDate=hour+'时'+min+'分'}
      }else{if(sec>0){createDate=hour+'时'+sec+'秒'}else{createDate=hour+'时'}
      }
    }else if(min>0){createDate=sec>0?(min+'分'+sec+'秒'):(min+'分')
    }else if(sec>0){createDate=sec+'秒'}
    return createDate
  }

时间转换 – 格式 1:1:11

Vue.filter('TimeFilter', function(value) {
  let hour,min,sec,createDate=value
  hour=parseInt(createDate/60/60)
  min=parseInt((createDate-hour*3600)/60)
  sec=parseInt(createDate-(hour*3600+min*60))
  if(hour>0){if(min>0){if(sec>0){createDate=hour+':'+min+':'+sec} else{createDate=hour+':'+min+':00'}
    }else{if(sec>0){createDate=hour+':00'+sec}else{createDate=hour+':00:00'}
    }
  }else if(min>0){createDate=sec>0?(min+':'+sec):(min+':00')
  }else if(sec>0){createDate=sec+'s'}
  return createDate
})

正文完
 0