时间格式转换 例如 1624351536 转 2023-04-18 下午4.45

文章介绍了如何使用JavaScript创建一个名为time.js的模块,用于处理和格式化时间戳。gettime函数根据时间差返回不同格式的时间显示,如当天、隔天和隔年的日期时间。在应用页面中,通过过滤器formaTime调用这个功能来展示用户新闻时间。
摘要由CSDN通过智能技术生成

新建一个time.js

//时间格式	
gettime(shorttime){
		shorttime=shorttime.toString().length<13 ? shorttime*1000 : shorttime;
		let now = (new Date()).getTime();
		let cha = (now-parseInt(shorttime))/1000;
		
		if (cha < 43200) {
			// 当天
			return this.dateFormat(new Date(shorttime),"{A} {t}:{ii}");
		} else if(cha < 518400){
			// 隔天 显示日期+时间
			return this.dateFormat(new Date(shorttime),"{Mon}月{DD}日 {A} {t}:{ii}");
		} else {
			// 隔年 显示完整日期+时间
			return this.dateFormat(new Date(shorttime),"{Y}-{MM}-{DD} {A} {t}:{ii}");
		}
	},
	

在要应用的页面,导入

	import $T from "@/common/time.js"

       // 过滤器  时间戳
    filters: {
		formaTime(value) {
			return $T.gettime(value)
		}
	},
	<!-- 昵称和 日期 -->
	<view class="list_one_shu">
             <!-- 昵称-->
			<view>{{item.username}}</view>
             <!-- 日期-->
			<view style="color: #a09997;">{{item.newstime | formaTime}}</view>
	</view>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值