VUE中根据后台传的时间戳转换成年月日,时分秒,星期三种格式

 后台传过来的时间戳,如果为10位 ,则需要*1000;时间戳为13位的话就不需要*1000。

//补位,如果为单数,则在前面加0
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const timestampToYmd=timestamp=>{
	let date = new Date(timestamp *1000) //时间戳为10位  需*1000,时间戳为13位的话就不需要*1000
	let year = date.getFullYear() 
	let month =date.getMonth() + 1
	let day = date.getDate()
	
	return [year,month,day].map(formatNumber).join('-')
}

const timestampToHms=timestamp=>{
  let date = new Date(timestamp *1000)
  const hour = date.getHours()
  const minute =date.getMinutes() + 1
  const second = date.getSeconds()
  return [hour, minute,second].map(formatNumber).join(':')
}

const timestampToDay=timestamp=>{
	let weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
	let date = new Date(timestamp *1000)
	let day =date.getDay()
	return weekDay[day]
}

export {
	timestampToDay,
	timestampToHms,
	timestampToYmd
	}

记录一下,以便查找,在页面中引用使用import,这几个函数是写在common.js中,一定要写对路径!!

import {
		timestampToHms,
		timestampToDay,
		timestampToYmd
	} from '../../../common.js'

 

你可能感兴趣的:(vue)