js实现后端返前端时间格式转换

6 篇文章 0 订阅

html代码
{{get_time(item.create_time)}}
在这里插入图片描述

js代码
get_time(dateTimeStamp) {
let minute = 1000 * 60;
let hour = minute * 60;
let day = hour * 24;
let halfamonth = day * 15;
let month = day * 30;
//若你得到的时间格式不是时间戳,可以使用下面的JavaScript函数把字符串转换为时间戳, 本函数的功能相当于JS版的strtotime:
let idata = Date.parse(dateTimeStamp.replace(/-/gi, “/”).replace(‘T’, ’ ')); //js函数代码:字符串转换为时间
let now = new Date().getTime();
let diffValue = now - idata;
if (diffValue < 0) {}
let monthC = diffValue / month;
let weekC = diffValue / (7 * day);
let dayC = diffValue / day;
let hourC = diffValue / hour;
let minC = diffValue / minute;
let result = ‘’
if (monthC >= 1) {
result = “发表于” + parseInt(monthC) + “个月前”;
} else if (weekC >= 1) {
result = “发表于” + parseInt(weekC) + “周前”;
} else if (dayC >= 1) {
result = “发表于” + parseInt(dayC) + “天前”;
} else if (hourC >= 1) {
result = “发表于” + parseInt(hourC) + “个小时前”;
} else if (minC >= 1) {
result = “发表于” + parseInt(minC) + “分钟前”;
} else
result = “刚刚发表”;
return result;
},

在这里插入图片描述
**

2--------------------------------------------

js代码:
imestampToTime(times, type) {
let Y = times.getFullYear() + ‘-’;
let M = (times.getMonth() + 1 < 10 ? ‘0’ + (times.getMonth() + 1) : times.getMonth() + 1) + ‘-’;
let D = (times.getDate() < 10 ? ‘0’ + times.getDate() : times.getDate()) + ’ ';
let h = (times.getHours() < 10 ? ‘0’ + times.getHours() : times.getHours()) + ‘:’;
let m = (times.getMinutes() < 10 ? ‘0’ + times.getMinutes() : times.getMinutes());
let s = (times.getSeconds() < 10 ? ‘:’ + ‘0’ + times.getSeconds() : ‘:’ + times.getSeconds());
switch (type) {
case “YY-MM-DD hh-mm”:
return Y + M + D + " " + h + m
break
}
}

在这里插入图片描述在这里插入图片描述在这里插入图片描述

**

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值