专注前端行业精选
小程序中 时间戳转日期格式(年月日时分秒)封装使用
作者:鹏仔先生日期:2020-11-25 11:26:36浏览:2655分类:JavaScript
在 utils 中自行新建文件 tool.js ,放入下放js代码
/** * function: 60秒内(刚刚),60秒至60分钟(**分钟前),1小时至24小时(**小时前),1天至15天(**天前),其他为正常日期显示 * @number 時間戳 */ function formatMsgTime(number) { var dateTime = new Date(number); // 将传进来的字符串或者毫秒转为标准时间 var Y = dateTime.getFullYear(); // 年 var M = dateTime.getMonth() + 1; // 月 var D = dateTime.getDate(); // 日 var h = dateTime.getHours(); // 时 var m = dateTime.getMinutes(); // 分 var millisecond = dateTime.getTime(); // 将当前编辑的时间转换为毫秒 var now = new Date(); // 获取本机当前的时间 var nowNew = now.getTime(); // 将本机的时间转换为毫秒 var milliseconds = 0; var numberStr; milliseconds = nowNew - millisecond; if (milliseconds <= 1000 * 60 * 1) { // 小于一分钟展示为刚刚 numberStr = '刚刚' } else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) { // 大于一分钟小于一小时展示为分钟 numberStr = Math.round((milliseconds / (1000 * 60))) + '分钟前' } else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) { // 大于一小时小于一天展示为小时 numberStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前' } else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) { // 大于一天小于十五天展示位天 numberStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前' } else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && Y === now.getFullYear()) { numberStr = M + '-' + D + ' ' + h + ':' + m } else { numberStr = Y + '-' + M + '-' + D + ' ' + h + ':' + m } return numberStr } /** * function: 時間戳轉日期 * @number 時間戳 * @type 格式(1為年-月-日 時-分-秒,2為年-月-日) */ function toDate(number, type) { var date = new Date(number); var Y = date.getFullYear(); var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(); var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(); var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); if (type == '1') { return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s; } else if (type == '2') { return Y + '-' + M + '-' + D; } } module.exports = { toDate: toDate, formatMsgTime: formatMsgTime }
formatMsgTime方法,传时间戳(Number)
60秒内——(刚刚)
60秒至60分钟——(**分钟前)
1小时至24小时——(**小时前)
1天至15天——(**天前)
其他为正常日期格式显示
toDate方法,传时间戳(Number)和(参数1、2)
1——年月日时分秒
2——年月日
页面需要使用,引入
import { toDate,formatMsgTime } from '../../utils/tool.js';
使用
formatMsgTime(Number) toDate(Number, 1)
formatMsgTime方法一般使用在发帖之类的地方,经常会看到刚刚发布,几分钟前发布,几小时前发布等
toDate方法一般比较常用,后台基本都是给前台反的时间戳,都需要进行转换
猜你还喜欢
- 02-23 百度小程序审核真麻烦呀
- 09-19 小程序 已被代码依赖分析忽略,无法被其他模块引用。你可根据控制台中的【代码依赖分析】告警信息修改代码,或关闭【过滤无依赖文件】功能
- 09-14 nvm常用命令有哪些?nvm如何切换node版本?nvm如何下载node?
- 08-19 使用HBuilderX将vue或H5项目打包app
- 07-15 小程序嵌入网页向小程序跳转并传参,微信小程序中实现公众号授权获取openId
- 07-13 vue中实现文件批量打包压缩下载(以及下载跨域问题分析)
- 07-08 uniapp调用地图,进行位置查询,标记定位
- 04-01 使用bat自动执行cmd命令(多个命令或单个命令)
- 11-05 js截取字符串前几位或者截取字符串后几位
- 10-25 js替换字符串某个字符,js修改字符串中指定字符
- 10-20 uniapp中text-indent不起作用,uniapp首行缩进不管用如何解决?
- 09-26 vue给单独组建的body添加类名
取消回复欢迎 你 发表评论:
- 搜索
- 随机tag
暂无评论,来添加一个吧。