uniapp 格式化时间刚刚,几分钟前,几小时前,几天前…

效果如图:

根目录下新建utils文件夹,文件夹下新增js文件,文件内容:

复制代码
export const filters = {
	dateTimeSub(data) {
		if (data == undefined) {
			return;
		}
		// 传进来的data必须是日期格式,不能是时间戳
		//将字符串转换成时间格式
		var timePublish = new Date(data);
		var timeNow = new Date();
		var minute = 1000 * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var month = day * 30;
		var result = "";
	
		var diffValue = timeNow - timePublish;  //时间差
		var diffMonth = diffValue / month;      //月差
		var diffWeek = diffValue / (7 * day);   //周差
		var diffDay = diffValue / day;          //天差
		var diffHour = diffValue / hour;        //时差
		var diffMinute = diffValue / minute;    //分差
	
		if (diffValue < 0) {
			console.log("错误时间");
		} else if (diffMonth > 3) {
			// result = timePublish.getFullYear() + "-";
			// result += timePublish.getMonth() + "-";
			// result += timePublish.getDate();
			// console.log(result);
		} else if (diffMonth > 1) {
			// result = parseInt(diffMonth) + "月前";
		} else if (diffWeek > 1) {
			// result = parseInt(diffWeek) + "周前";
		} else if (diffDay > 3) {
			let y = timePublish.getFullYear();
			let MM = timePublish.getMonth() + 1;
			MM = MM < 10 ? ('0' + MM) : MM; //月补0
			let d = timePublish.getDate();
			d = d < 10 ? ('0' + d) : d; //天补0
			result = y + '-' + MM + '-' + d;
		} else if (diffDay>=1 && diffDay <= 3) {
			result = parseInt(diffDay) + "天前";
		} else if (diffHour > 1) {
			result = parseInt(diffHour) + "小时前";
		} else if (diffMinute > 1) {
			result = parseInt(diffMinute) + "分钟前";
		} else {
			result = "刚刚";
		}
		return result;
	},
    formatDate(value) {
    	if (value == undefined) {
    		return;
    	}
    	// #ifdef APP-PLUS
    		if(plus.os.name == 'iOS') {
    			time = time.replaceAll("-", "/")
    		}
    	// #endif
    	// let date = new Date(value * 1000);
    	let date = new Date(value);
    	//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    	let y = date.getFullYear();
    	let MM = date.getMonth() + 1;
    	MM = MM < 10 ? ('0' + MM) : MM; //月补0
    	let d = date.getDate();
    	d = d < 10 ? ('0' + d) : d; //天补0
    	let h = date.getHours();
    	h = h < 10 ? ('0' + h) : h; //小时补0
    	let m = date.getMinutes();
    	m = m < 10 ? ('0' + m) : m; //分钟补0
    	let s = date.getSeconds();
    	s = s < 10 ? ('0' + s) : s; //秒补0
    	return y + '-' + MM + '-' + d; //年月日
    	// return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s; //年月日时分秒
    },
    formatTime(value) {
    	if (value == undefined) {
    		return;
    	}
    	// #ifdef APP-PLUS
    		if(plus.os.name == 'iOS') {
    			time = time.replaceAll("-", "/")
    		}
    	// #endif
    	let date = new Date(value);
    	let MM = date.getMonth() + 1;
    	MM = MM < 10 ? ('0' + MM) : MM; //月补0
    	let d = date.getDate();
    	d = d < 10 ? ('0' + d) : d; //天补0
    	return  MM + '月' + d + '日'; //年月日
    }
}

main.js中引入

复制代码
import Vue from 'vue'

Vue.config.productionTip = false

import { filters } from './utils/time_day.js'
// 定义全局自定义过滤器
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
})

页面中使用即可

复制代码
<view class="times">{{item.updatetime | getDateDiff}}</view>
相关推荐
结网的兔子5 小时前
【前端开发】Web端迁移至 uni-app 及鸿蒙扩展方案对比
前端·uni-app·harmonyos
2501_915909066 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
AI多Agent协作实战派8 小时前
AI多Agent协作系统实战(二十八):顶栏统一战争——从1个页面异常到31个页面全量对齐
数据库·人工智能·uni-app
00后程序员张9 小时前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
2501_916008891 天前
iOS IPA文件反编译与打包操作方法,拆包分析防护和加固打包
android·macos·ios·小程序·uni-app·cocoa·iphone
name好难取诶1 天前
通过对云基座和离线基座的对比,帮助开发者理解如何在不同场景下选择合适的打包方案。 什么是“基座” 在 UniApp 中,基座(也称“ ...
开发语言·uni-app·php
2501_915921431 天前
iOS手游安全反外挂解决方案:基于Xcode的加固混淆特色详解
android·安全·ios·小程序·uni-app·iphone·xcode
2501_916007471 天前
iOS App分发教程 App Store、TestFlight 与 Ad Hoc 的配置与上传方法
android·ios·小程序·https·uni-app·iphone·webview
qq_316837751 天前
uniapp 打包APP 使用 Galacean Effects 的特效
uni-app
古韵2 天前
你的 uni-app 分页,一个 hook 搞定竞态和加载
javascript·vue.js·uni-app