首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >如何将时间戳转为时间对象

如何将时间戳转为时间对象

作者头像
程序媛夏天
发布2024-01-18 20:08:16
发布2024-01-18 20:08:16
1.1K0
举报

在这次智小窝开发项目过程中,在对接数据的时候,有一些经过时间戳转换的数据 比如rentalBeginDate: 1564588800 ,rentalEndDate: 1567267199 需要将它转换为时间对象, 参考下面为一个时间戳函数,可以直接套用哦!

代码语言:javascript
复制
	/** js调用时间戳*/
	const {
		rentalBeginDate,
		rentalEndDate,
		receivableDate
	} = item;                     //解构赋值
	item.rentalBeginDate = this.formatDateTime(rentalBeginDate);
	item.rentalEndDate = this.formatDateTime(rentalEndDate);
	item.receivableDate = this.formatDateTime(receivableDate);
	
	/** 时间戳函数 */
	formatDateTime(timeStamp) {
		let date = new Date(timeStamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘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();
		return Y + M + D;
	},
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档