时间戳转化及日期星期计算

一、
1、时间格式化

this.time是时分,例如12:23
let subTimeFormate = this.format(this.time,2)

2、计算星期

//值从0到6
let nowWeek = nowDate.getDay();
let arr=['周日','周一','周二','周三','周四','周五','周六']
let week_one=arr[nowWeek]
return week_one //返回的是周几

二、几个小时的时间戳转化为时分秒

//data为时间戳,data的单位为秒
function time1(data){
	var h = Math.floor(data/60/60);
	var m = Math.floor(data/60%60);
	var s = Math.floor(data%60);
	return h + '时' + m + '分' + s + '秒'
}

三、通过时间戳来算出发布时间
time是十位数

var timeone =time*1000
//当发布的时间距离当前时间超过24小时,就显示年月日
if(new Date().getTime()-timeone>86400000){
	const times = new Date(timeone);
	const Y = times.getFullYear();
	console.log(Y)
	let M = times.getMonth() + 1;
	M = M < 10 ? '0' + M : M;
	let D = times.getDate();
	D = D < 10 ? '0' + D : D;
	return `${Y}年${M}月${D}日`
}
//当发布的时间距离当前时间不超过24小时,就显示秒分小时
else{
	const duration = (+new Date().getTime() - timeone) / 1000;
	let displayTime = ''
	if (duration < 60) {
		console.log('秒')
		displayTime = `${Math.round(Math.max(duration, 1))} 秒前`;
	}else if (duration < 60*60) {
		console.log('分')
		displayTime = `${Math.round(duration / 60)} 分钟前`;
	}else if (duration < 60*60*24) {
		console.log('小时')
		displayTime = `${Math.round(duration / 60 / 60)} 小时前`;
	}else {
		displayTime = dateTime(recordTime);
	}
	return displayTime
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值