uni-app 获取时间

608 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第10天,查看详情

需求:

image.png

时间格式化

//时间格式化
function formatterDate(date, fmt) {
	let nowDate = {
		yyyy: date.getFullYear(), // 年
		MM: date.getMonth() + 1, // 月份
		dd: date.getDate(), //日
		hh: date.getHours(),
		mm: date.getMinutes(),
		ss: date.getSeconds()
	}
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in nowDate) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (nowDate[k]) : (("00" + nowDate[k]).substr(("" +
				nowDate[k]).length)));
		}
	}
	return fmt;
}

1.获取现在的时间

export function getNowFormatDate() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	// 比如2022-10-10 16:15:00
	// var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	// 	+
	// 	" " + date.getHours() + seperator2 + date.getMinutes()
	// 	+
	// 	seperator2 + date.getSeconds();

	// 比如2022-10-10
	var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	return currentdate
	console.log('currentdate', currentdate)
}

2. 获取一周之前的时间

export function getWeekDate() {
	let oneDay = 24 * 60 * 60 * 1000
	let startTime = new Date(Date.now() - 6 * oneDay)
	startTime = formatterDate(startTime, "yyyy-MM-dd")
	return startTime
}

3. 获取一个月的时间

export function getMonthDate() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth();
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	console.log('strDate', strDate)
	var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	return currentdate
	console.log('currentdate', currentdate)
}

4. 获取半年的时间

export function getQuarterlyDate() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";

	if ((date.getMonth() + 1) == 1) {
		var month = 10
	} else if ((date.getMonth() + 1) == 2) {
		var month = 11;
	} else if ((date.getMonth() + 1) == 3) {
		var month = 12;
	} else {
		var month = (date.getMonth() + 1) - 3;
	}
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	console.log('strDate', strDate)
	// 比如2022-10-10
	// 
	var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
	return currentdate
	console.log('currentdate', currentdate)
}

5. 获取一年的时间

export function getYearDate() {
	var date = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	console.log('strDate', strDate)
	// 比如2022-10-10
	// 
	var currentdate = (date.getFullYear() - 1) + seperator1 + month + seperator1 + strDate
	return currentdate
	console.log('currentdate', currentdate)
}

页面上引用

import {
		getWeekDate,
		getMonthDate,
		getQuarterlyDate,
		getYearDate,
		getNowFormatDate
	} from '@/utils/array.js';
       this.beginDate = getWeekDate()