首页 > 其他分享 >js获取时间文件的封装

js获取时间文件的封装

时间:2022-10-27 13:44:20浏览次数:160  
标签:封装 format js 60 获取 time date return else

/*
 * 年(Y) 可用1-4个占位符
 * 月(m)、日(d)、小时(H)、分(M)、秒(S) 可用1-2个占位符
 * 星期(W) 可用1-3个占位符
 * 季度(q为阿拉伯数字,Q为中文数字)可用1或4个占位符
 *
 * let date = new Date()
 * formatDate(date, "YYYY-mm-dd HH:MM:SS")           // 2020-02-09 14:04:23
 * formatDate(date, "YYYY-mm-dd HH:MM:SS Q")         // 2020-02-09 14:09:03 一
 * formatDate(date, "YYYY-mm-dd HH:MM:SS WWW")       // 2020-02-09 14:45:12 星期日
 * formatDate(date, "YYYY-mm-dd HH:MM:SS QQQQ")      // 2020-02-09 14:09:36 第一季度
 * formatDate(date, "YYYY-mm-dd HH:MM:SS WWW QQQQ")  // 2020-02-09 14:46:12 星期日 第一季度
 */
export function formatDate(date, format) {
	let we = date.getDay(); // 星期
	let qut = Math.floor((date.getMonth() + 3) / 3).toString(); // 季度
	const opt = {
		'Y+': date.getFullYear().toString(), // 年
		'm+': (date.getMonth() + 1).toString(), // 月(月份从0开始,要+1)
		'd+': date.getDate().toString(), // 日
		'H+': date.getHours().toString(), // 时
		'M+': date.getMinutes().toString(), // 分
		'S+': date.getSeconds().toString(), // 秒
		'q+': qut, // 季度
	};
	const week = {
		// 中文数字 (星期)
		'0': '日',
		'1': '一',
		'2': '二',
		'3': '三',
		'4': '四',
		'5': '五',
		'6': '六',
	};
	const quarter = {
		// 中文数字(季度)
		'1': '一',
		'2': '二',
		'3': '三',
		'4': '四',
	};
	if (/(W+)/.test(format)) {
		format = format.replace(RegExp.$1, RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' + week[we] : '周' + week[we]) : week[we]);
	}
	if (/(Q+)/.test(format)) {
		// 输入一个Q,只输出一个中文数字,输入4个Q,则拼接上字符串
		format = format.replace(RegExp.$1, RegExp.$1.length == 4 ? '第' + quarter[qut] + '季度' : quarter[qut]);
	}
	for (let k in opt) {
		let r = new RegExp('(' + k + ')').exec(format);
		if (r) {
			// 若输入的长度不为1,则前面补零
			format = format.replace(r[1], RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0'));
		}
	}
	return format;
}

/**
 * 10秒:  10 * 1000
 * 1分:   60 * 1000
 * 1小时: 60 * 60 * 1000
 * 24小时:60 * 60 * 24 * 1000
 * 3天:   60 * 60* 24 * 1000 * 3
 *
 * let data = new Date()
 * formatPast(data)                                           // 刚刚
 * formatPast(data - 11 * 1000)                               // 11秒前
 * formatPast(data - 2 * 60 * 1000)                           // 2分钟前
 * formatPast(data - 60 * 60 * 2 * 1000)                      // 2小时前
 * formatPast(data - 60 * 60 * 2 * 1000)                      // 2小时前
 * formatPast(data - 60 * 60 * 71 * 1000)                     // 2天前
 * formatPast("2020-06-01")                                   // 2020-06-01
 * formatPast("2020-06-01", "YYYY-mm-dd HH:MM:SS WWW QQQQ")   // 2020-06-01 08:00:00 星期一 第二季度
 */
export function formatPast(param, format = 'YYYY-mm-dd') {
	// 传入格式处理、存储转换值
	let t, s;
	// 获取js 时间戳
	let time = new Date().getTime();
	// 是否是对象
	typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param);
	// 当前时间戳 - 传入时间戳
	time = Number.parseInt(time - t);
	if (time < 10000) {
		// 10秒内
		return '刚刚';
	} else if (time < 60000 && time >= 10000) {
		// 超过10秒少于1分钟内
		s = Math.floor(time / 1000);
		return `${s}秒前`;
	} else if (time < 3600000 && time >= 60000) {
		// 超过1分钟少于1小时
		s = Math.floor(time / 60000);
		return `${s}分钟前`;
	} else if (time < 86400000 && time >= 3600000) {
		// 超过1小时少于24小时
		s = Math.floor(time / 3600000);
		return `${s}小时前`;
	} else if (time < 259200000 && time >= 86400000) {
		// 超过1天少于3天内
		s = Math.floor(time / 86400000);
		return `${s}天前`;
	} else {
		// 超过3天
		let date = typeof param === 'string' || 'object' ? new Date(param) : param;
		return formatDate(date, format);
	}
}

/**
 * formatAxis(new Date())   // 上午好
 */
export function formatAxis(param) {
	let hour = new Date(param).getHours();
	if (hour < 6) {
		return '凌晨好';
	} else if (hour < 9) {
		return '早上好';
	} else if (hour < 12) {
		return '上午好';
	} else if (hour < 14) {
		return '中午好';
	} else if (hour < 17) {
		return '下午好';
	} else if (hour < 19) {
		return '傍晚好';
	} else if (hour < 22) {
		return '晚上好';
	} else {
		return '夜里好';
	}
}

标签:封装,format,js,60,获取,time,date,return,else
From: https://www.cnblogs.com/axingya/p/16831926.html

相关文章

  • python获取文件夹、文件大小&hiberfil.sys
    跑了个很简单的程序,c盘突然爆炸增加,顿时SSD120G的盘只剩下几个G,所以有需求看看那些文件占用了大量空间。(事先通过360大文件扫描、日期排序分析未发现异常)importosimportsy......
  • Fastjson反序列化解析流程分析(以TemplatesImpl加载字节码过程为例)
    文章目录​​写在前面​​​​流程分析​​写在前面关于TemplatesImpl加载字节码就不多说了,之前也写过自己翻一翻,或者网上看看其他大佬的,至于为什么选择这一个,因为这里面大......
  • 【JS】对象属性标志和属性描述符
    1.对象属性对象属性分为两种:数据属性访问器属性2.对象数据属性标志属性标志有4个:value、writable、enumberable、configurablevalue:  即属性值writable: ......
  • 【JSON】Python读取JSON文件报错json.decoder.JSONDecodeError的问题
    报错json.decoder.JSONDecodeError:Expectingpropertynameenclosedindoublequotes:line*column*(char*)解决百度到了多种情况:编码使用UTF-8键值用双引......
  • leetcode 206. 反转链表 js实现
    给你单链表的头节点head,请你反转链表,并返回反转后的链表。 示例1:输入:head=[1,2,3,4,5]输出:[5,4,3,2,1]示例2:输入:head=[1,2]输出:[2,1]示例3:输入:head=[]输出......
  • js async await
    async函数一定会返回一个promise对象。如果一个async函数的返回值看起来不是promise,那么它将会被隐式地包装在一个promise中。如asyncfunctionfoo(){retur......
  • Node.JS 安装(Windows 和 Linux )
    1、Linux版采用YUM方式安装。1.1、卸载旧版本查看旧版本:rpm-qa|grepnodejs卸载:卸载过程中输入y确认yumremovenodejs1.2、安装1.2.1、Hint官......
  • 【Spark 3.0-JavaAPI-pom】体验JavaRDD函数封装变化
    一、pom<properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><scala.version>2.......
  • 打印的js
    //打印类属性、方法定义/*eslint-disable*/constPrint=function(dom,options){if(!(thisinstanceofPrint))returnnewPrint(dom,options);this.op......
  • 项目实战:在线报价采购系统(React +SpreadJS+Echarts)
    小伙伴们对采购系统肯定不陌生,小到出差路费、部门物资采购;大到生产计划、原料成本预估都会涉及到该系统。管理人员可以通过采购系统减少管理成本,说是管理利器毫不过分,对于......