vue uniapp 小程序 判断日期是今天(显示时分秒)、昨天、本周的周几、超出本周显示年月日

效果图:

util.js

bash 复制代码
/**
 * 转换时间
 */
const messageFormat = (datetime) =>{	
	let result = "";
	let currentTime = new Date();
	if(isToday(datetime)){
		result = datetime.substring(11,16);
	}else if(isYesterday(datetime)){
		result = "昨天";
	}else if(isCurrentWeek(datetime)){
		let day = new Date(datetime).getDay();
		switch (day)
		{
			case 1:
				result = "星期一";
				break;
			case 2:
				result = "星期二";
				break;
			case 3:
				result = "星期三";
				break;
			case 4:
				result = "星期四";
				break;
			case 5:
				result = "星期五";
				break;
			case 6:
				result = "星期六";
				break;
			default:
				result = "星期日";
		}
	}else{
		result = datetime.substring(0,10);
	}	
	return result;
}

//是否为今天
function isToday(str){
    var d = new Date(str.replace(/-/g,"/"));
    var todaysDate = new Date();
    if(d.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)){
        return true;
    } else {
        return false;
    }
}

//是否为昨天
function isYesterday(str){
    var d = new Date(str.replace(/-/g,"/"));
    var todaysDate = new Date();
	todaysDate.setDate(todaysDate.getDate()-1);
    if(d.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)){
        return true;
    } else {
        return false;
    }
}
//是否为本周内的一天
function isCurrentWeek(str){
	var d = new Date(str.replace(/-/g,"/"));
    var todaysDate = new Date();
	var firstdayoffset = (todaysDate.getDay()==0?7:todaysDate.getDay())-1;
	todaysDate.setDate(todaysDate.getDate()-firstdayoffset-1);
    var firstday = todaysDate.toISOString().substring(0,10);
	firstday = new Date(firstday);
	todaysDate.setDate(todaysDate.getDate()+8);
	var lastday = todaysDate.toISOString().substring(0,10);
	lastday = new Date(lastday);
	if((d.setHours(0,0,0,0) > firstday.setHours(0,0,0,0)) && (d.setHours(0,0,0,0) < lastday.setHours(0,0,0,0))){
        return true;
    } else {
        return false;
    }
}

module.exports = {
	messageFormat
}

script

bash 复制代码
<script>
	import {messageFormat} from "@/utils/util.js"
	export default {
		data() {
			return {
				myApplicationQty: 0, //我的用车数量
				myApplicationQtyTime: null, //我的用车时间
			}
		}
</script>

template

bash 复制代码
<view class="item-time-count">
	<view class="time" v-if="myApplicationQty != 0">{{myApplicationQtyTime}}</view>
	<view class="mes-count" v-if="myApplicationQty != 0">{{myApplicationQty > 99 ? '99+' : myApplicationQty}}</view>
</view>

methods

bash 复制代码
methods: {
	//请求接口返回数据(此处省略)
	//对返回的时间数据进行转换时间处理
	if(res.data.myApplicationQtyTime){
		// 转换时间
		this.myApplicationQtyTime = messageFormat(res.data.myApplicationQtyTime)
	}
}
相关推荐
样子201810 小时前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
fakaifa12 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
杨荧12 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
Bug改不动了14 小时前
React Native 与 UniApp 对比
react native·react.js·uni-app
南半球与北海道#15 小时前
前端引入vue-super-flow流程图插件
前端·vue.js·流程图
BillKu15 小时前
vue3+element-plus 输入框el-input设置背景颜色和字体颜色,样式效果等同于不可编辑的效果
前端·javascript·vue.js
每天学习一丢丢15 小时前
Spring Boot + Vue 项目用宝塔面板部署指南
vue.js·spring boot·后端
springfe010115 小时前
vue3组件 - 大文件上传
前端·vue.js
好好好明天会更好16 小时前
uniapp项目中小程序的生命周期
前端·vue.js
萌萌哒草头将军16 小时前
有了它 ,我彻底告别了 try-finally 🔥🔥🔥
前端·javascript·vue.js