JS 对时间日期的转换与操作
admin
2024-04-14 13:44:38
0

JS 时间格式转换

this.timeFormatConvert(new Date()) // 2022-11-26 21:37:29

/** 时间格式转换* @param e 要转换的日期(如:Sat Nov 26 2022 21:37:29 GMT+0800 (中国标准时间))* @returns {string} 转换结果(如:2022-11-26 21:37:29)*/
timeFormatConvert(e) {const Y = e.getFullYear(); // 年const M = this.prefixZero(e.getMonth() + 1); // 月const D = this.prefixZero(e.getDate()); // 日const H = this.prefixZero(e.getHours()); // 时const Mi = this.prefixZero(e.getMinutes()); // 分const S = this.prefixZero(e.getSeconds()); // 秒return Y + "-" + M + "-" + D + " " + H + ":" + Mi + ":" + S;
},
prefixZero(num = 0, n = 2) {// 数字位数不够,数字前面补零return (Array(n).join("0") + num).slice(-n);
},

JS 获取当前年月日时分秒

this.getCurrTime("noGap") // 20221129154229

this.getCurrTime() // 2022-11-29 15:44:20

/** 获取当前年月日时分秒* @param e 默认.年月日时分秒 date.年月日 dateTime.年月日时分 noGap.无分隔符的年月日时分秒* @returns {string}*/
getCurrTime(e = "") {const curDate = new Date(),T = {Y: curDate.getFullYear().toString(), // 年M: this.prefixZero(curDate.getMonth() + 1), // 月D: this.prefixZero(curDate.getDate()), // 日H: this.prefixZero(curDate.getHours()), // 时Mi: this.prefixZero(curDate.getMinutes()), // 分S: this.prefixZero(curDate.getSeconds()), // 秒},tDate = T.Y + "-" + T.M + "-" + T.D; // 年月日let result = tDate + " " + T.H + ":" + T.Mi + ":" + T.S; // 年月日时分秒if (e === "date") result = tDate; // 年月日if (e === "dateTime") result = tDate + " " + T.H + ":" + T.Mi; // 年月日时分if (e === "noGap") result = T.Y + T.M + T.D + T.H + T.Mi + T.S; // 无分隔符return result;
},
prefixZero(num = 0, n = 2) {// 数字位数不够,数字前面补零return (Array(n).join("0") + num).slice(-n);
},

JS 获取n天前或n天后的日期

当前日期:2022-11-27

7天后的日期:this.getAgoLaterDate(7) // 2022-12-04

7天前的日期:this.getAgoLaterDate(-7) // 2022-11-20

/** 获取n天前或n天后的日期(默认当天)* @param n 需要的天数(+n之后的日期,-n之前的日期)* @returns {string} 默认返回当前日期*/
getAgoLaterDate(n = 0) {const curTime = new Date(),D1 = curTime.getDate(),date = new Date(curTime);date.setDate(D1 + n);const Y2 = date.getFullYear(),M2 = this.prefixZero(date.getMonth() + 1),D2 = this.prefixZero(date.getDate());return Y2 + "-" + M2 + "-" + D2;
},
prefixZero(num = 0, n = 2) {// 数字位数不够,数字前面补零return (Array(n).join("0") + num).slice(-n);
},

JS 获取指定日期的周一和周日

this.getSpecifyMonSun() // {mon: '2022-11-21', sun: '2022-11-27'}

this.getSpecifyMonSun("2022/11/17") // {mon: '2022-11-14', sun: '2022-11-20'}

/** 获取指定日期的周一和周日(默认当前日期)* @param e 需要指定的日期(格式:yyyy-MM-dd 或 yyyy/MM/dd 或 yyyy.MM.dd)* @returns {{mon: string, sun: string}} 默认返回当前日期的周一和周末日期*/
getSpecifyMonSun(e = new Date()) {const that = this,now = new Date(e),nowTime = now.getTime(),day = now.getDay() || 7, // 周日时day修改为7,否则获取的是下周一到周日的日期oneDayTime = 24 * 60 * 60 * 1000, // 一天的毫秒(ms)数mondayTime = new Date(nowTime - (day - 1) * oneDayTime), //周一的日期sundayTime = new Date(nowTime + (7 - day) * oneDayTime); //周日的日期function dataFormat(d) {const Y = d.getFullYear(), // 年M = that.prefixZero(d.getMonth() + 1), // 月D = that.prefixZero(d.getDate()); // 日return Y + "-" + M + "-" + D;}return { mon: dataFormat(mondayTime), sun: dataFormat(sundayTime) };
},
prefixZero(num = 0, n = 2) {// 数字位数不够,数字前面补零return (Array(n).join("0") + num).slice(-n);
},

JS 获取指定日期的月初和月末

this.getSpecifyEarlyEndMonth() // {first: '2022-11-01', end: '2022-11-30'}

this.getSpecifyEarlyEndMonth("2022/09") // {first: '2022-09-01', end: '2022-09-30'}

/** 获取指定日期的月初和月末(默认当前月)* @param e 需要指定的日期(格式:yyyy-MM 或 yyyy/MM 或 yyyy.MM)* @returns {{end: string, first: string}} 默认返回当前月的月初和月末日期*/
getSpecifyEarlyEndMonth(e = new Date()) {const that = this,nowDate = new Date(e), // 当前日期year = nowDate.getFullYear(), // 指定年month = nowDate.getMonth() + 1, // 指定月oneDayTime = 24 * 60 * 60 * 1000, // 一天的毫秒(ms)数firstDay = new Date(year, month - 1), // 当月第一天的日期lastDay = new Date(new Date(year, month).valueOf() - oneDayTime); // 当月最后一天的日期function dataFormat(d) {const Y = d.getFullYear(), // 年M = that.prefixZero(d.getMonth() + 1), // 月D = that.prefixZero(d.getDate()); // 日return Y + "-" + M + "-" + D;}return { first: dataFormat(firstDay), end: dataFormat(lastDay) };
},
prefixZero(num = 0, n = 2) {// 数字位数不够,数字前面补零return (Array(n).join("0") + num).slice(-n);
},

JS 根据当前时间判断是早上、中午、下午

this.getCurTimeState() // 晚上好

/** 根据当前时间判断是早上、中午、下午* @returns {string} 返回当前时间段对应的状态*/
getCurTimeState() {const nowTime = new Date(), // 获取当前时间hour = nowTime.getHours(); // 获取当前小时let result = ""; // 设置默认文字// 判断当前时间段if (hour >= 0 && hour <= 10) {result = "早上好";} else if (hour > 10 && hour <= 14) {result = "中午好";} else if (hour > 14 && hour <= 18) {result = "下午好";} else if (hour > 18 && hour <= 24) {result = "晚上好";}return result; // 返回当前时间段对应的状态
},

JS 将年月日时分秒转化为刚刚,几分钟前,几小时前,几天前等

this.timeConvertReveal("2020-11-27 14:38:16") // 2年前

this.timeConvertReveal() // 刚刚

/** 将年月日时分秒转化为刚刚,几分钟前,几小时前,几天前等(默认刚刚)* @param e 需要转换的时间(格式:yyyy-MM-dd HH:mm:ss 或 yyyy/MM/dd HH:mm:ss 或 yyyy.MM.dd HH:mm:ss)* @returns {string} 默认返回刚刚*/
timeConvertReveal(e = new Date()) {const minute = 1000 * 60, // 分钟hour = minute * 60, // 小时day = hour * 24, // 天week = day * 7, // 周month = day * 30, // 月year = month * 12, // 年curTime = new Date().getTime(), // 当前时间的时间戳specifyTime = new Date(e), // 指定时间的时间戳mistiming = curTime - specifyTime, // 时间差yearDiffer = mistiming / year, // 年差monthDiffer = mistiming / month, // 月差weekDiffer = mistiming / week, // 周差dayDiffer = mistiming / day, // 天差hourDiffer = mistiming / hour, // 时差minuteDiffer = mistiming / minute; // 分钟差let result = "-";if (yearDiffer >= 1) result = toInt(yearDiffer) + "年前";else if (monthDiffer >= 1) result = toInt(monthDiffer) + "月前";else if (weekDiffer >= 1) result = toInt(weekDiffer) + "周前";else if (dayDiffer >= 1) result = toInt(dayDiffer) + "天前";else if (hourDiffer >= 1) result = toInt(hourDiffer) + "小时前";else if (minuteDiffer >= 1) result = toInt(minuteDiffer) + "分钟前";else result = "刚刚";function toInt(num) {return parseInt(num.toString());}return result;
},

JS 获得指定月份的天数

this.getMonthDays("2022-10") // 31

/** 获得指定月份的天数(默认当前月)* @param e 需要指定的日期(格式:yyyy-MM 或 yyyy/MM 或 yyyy.MM)* @returns {number} 默认返回当前月的天数*/
getMonthDays(e = new Date()) {const now = new Date(e), // 指定日期nowYear = now.getYear(), // 指定年份nowMonth = now.getMonth(), // 指定月份startDate = new Date(nowYear, nowMonth, 1), // 开始日期endDate = new Date(nowYear, nowMonth + 1, 1); // 结束日期return (endDate - startDate) / (1000 * 60 * 60 * 24);
},

JS 获取指定日期是周几

this.getCurrWeek("2022-11-24") // 周四

this.getCurrWeek() // 周二

/** 获取指定日期是周几* @param e 需要指定的日期(格式:yyyy-MM-dd 或 yyyy/MM/dd 或 yyyy.MM.dd)* @returns {string} 默认返回当前日期是周几*/
getCurrWeek(e = new Date()) {const day = new Date(e).getDay(),weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];return weeks[day];
},

JS 计算两个时间的时间差

this.getTimeDiff("2022-11-28 19:30:24") // {text: '时间已超过30分钟', timeDiff: '0天21时28分40秒40毫秒', dateObj: {day: 0, hour: 21, min: 28, sec: 40, ms: 40}}

/** 计算两个时间的时间差(timeDiff返回格式为xx天xx时xx分xx秒xx毫秒)* @param startTime 开始时间(格式:yyyy-MM-dd HH:mm:ss 或 yyyy/MM/dd HH:mm:ss 或 yyyy.MM.dd HH:mm:ss [需小于结束时间])* @param endTime 结束时间(格式:yyyy-MM-dd HH:mm:ss 或 yyyy/MM/dd HH:mm:ss 或 yyyy.MM.dd HH:mm:ss [默认当前时间])* @returns {string|{timeDiff: string, text: string, dateObj: {sec: number, min: number, hour: number, ms: number, day: number}}}*/
getTimeDiff(startTime, endTime = new Date()) {if (!startTime) return "错误:开始时间为必填";const diffVal = new Date(endTime) - new Date(startTime); // 两个时间戳相差的毫秒数if (diffVal < 0) return "错误:开始时间大于结束时间";const day = Math.floor(diffVal / (24 * 3600 * 1000)), // 计算相差的天数remainDay = diffVal % (24 * 3600 * 1000), // 计算天数后剩余的毫秒数hour = Math.floor(remainDay / (3600 * 1000)), // 计算相差的小时数remainHour = remainDay % (3600 * 1000), // 计算小时数后剩余的毫秒数min = Math.floor(remainHour / (60 * 1000)), // 计算相差的分钟数remainMinute = remainHour % (60 * 1000), // 计算分钟数后剩余的毫秒数sec = Math.round(remainMinute / 1000), // 计算相差的秒数remainSeconds = remainMinute % (60 * 1000), // 计算秒数后剩余的毫秒数ms = Math.round(remainSeconds / 1000), // 计算相差的毫秒数time = day + "天" + hour + "时" + min + "分" + sec + "秒" + ms + "毫秒",obj = { day: day, hour: hour, min: min, sec: sec, ms: ms };let val = "时间未超过30分钟";if (min > 30 || hour > 1) val = "时间已超过30分钟";return { text: val, timeDiff: time, dateObj: obj };
},

JS 从日期字符串中截取出年、月、日、时、分、秒

this.captureTime() // {day: "29", hour: "17", minute: "40", month: "11", second: "06", year: "2022"}

this.captureTime("2020-12-11 14:54:48") // {day: "11", hour: "14", minute: "54", month: "12", second: "48", year: "2020"}

/** 从日期字符串中截取出年、月、日、时、分、秒* @param e 需要截取的日期(格式:yyyy-MM-dd HH:mm:ss 或 yyyy/MM/dd HH:mm:ss 或 yyyy.MM.dd HH:mm:ss [默认当前日期])* @returns {{month: *, hour: *, year: *, day: *, minute: *, second: *}} 默认返回当前日期*/
captureTime(e = null) {let time = e;if (!e) {const curDate = new Date(),T = {Y: curDate.getFullYear().toString(), // 年M: this.prefixZero(curDate.getMonth() + 1), // 月D: this.prefixZero(curDate.getDate()), // 日H: this.prefixZero(curDate.getHours()), // 时Mi: this.prefixZero(curDate.getMinutes()), // 分S: this.prefixZero(curDate.getSeconds()), // 秒};time = T.Y + "-" + T.M + "-" + T.D + " " + T.H + ":" + T.Mi + ":" + T.S;}const timeArr = time.replace(/[:. /]/g, "-").split("-"); // 将":. /"换为"-",再根据"-"分组return {year: timeArr[0], // 年month: timeArr[1], // 月day: timeArr[2], // 日hour: timeArr[3], // 时minute: timeArr[4], // 分second: timeArr[5], // 秒};
},
prefixZero(num = 0, n = 2) {// 数字位数不够,数字前面补零return (Array(n).join("0") + num).slice(-n);
},

相关内容

热门资讯

安卓作业系统耗电,深度解析耗电... 手机电量告急,是不是你也和我一样,对安卓作业系统的耗电问题头疼不已?别急,今天就来聊聊这个让人又爱又...
中国能替代安卓系统,中国自主研... 你有没有想过,如果有一天,你的手机上不再是那个熟悉的安卓系统,而是中国自主研发的系统呢?想象是不是有...
怎么安装安卓系统车机,安卓系统... 你有没有想过,你的爱车也能变身成为一部智能移动设备?没错,就是那种可以安装安卓系统的车机!想象在驾驶...
安卓平板如何转系统版本,安卓平... 你有没有发现,你的安卓平板用久了,系统好像有点卡卡的呢?别急,今天就来教你怎么轻松地给平板升级系统版...
官方安卓系统去哪下载,一站式下... 你有没有想过,你的安卓手机里那个熟悉的官方安卓系统,其实背后有着一个神秘的世界?今天,就让我带你一探...
安卓系统上安装pc软件,跨平台... 你是不是也和我一样,对安卓系统上的那些PC软件垂涎欲滴呢?想象在手机上就能享受到电脑上的强大功能,是...
安卓系统官网rom下载,轻松获... 你有没有发现,安卓系统的魅力真的是无处不在呢?无论是手机、平板还是智能穿戴设备,安卓系统都以其强大的...
如何修改安卓系统调度,安卓系统... 你有没有想过,你的安卓手机其实就像一个超级英雄,每天都在默默无闻地为你处理各种任务?但是,你知道吗?...
安卓系统的百度,百度引领智能科... 你知道吗?在智能手机的世界里,安卓系统就像是个万能的大舞台,而百度,就像是那个舞台上最亮眼的明星。今...
安卓系统软件怎么用,从入门到精... 你手里那台安卓手机是不是感觉功能强大,却又有点儿摸不着头脑呢?别急,今天就来手把手教你如何玩转安卓系...
杂牌平板刷安卓原生系统,体验流... 你有没有想过,那些看似普通的杂牌平板,竟然也能刷上安卓原生系统?听起来是不是有点不可思议?别急,今天...
多可文档系统安卓下载,提升办公... 你有没有想过,在手机上轻松管理各种文档,是不是一件超级酷的事情呢?现在,我就要给你介绍一个超实用的多...
安卓70系统流畅吗,畅享无忧 你有没有注意到,最近安卓70系统(也就是Nougat)在手机圈里可是掀起了一阵热潮呢!不少朋友都在问...
推特安卓系统如何下载,如何在推... 探秘推特安卓系统的神秘下载之旅在数字化时代,社交媒体以其独特的魅力,让我们与世界保持紧密联系。而推特...
哪个最接近原装安卓系统,深度解... 你有没有想过,手机里的安卓系统,哪个最像原装的呢?是不是每次更新后,都感觉有点不一样,少了点什么?别...
安卓系统与app软件,安卓系统... 你有没有发现,现在手机里装满了各种各样的app软件?而这些app软件,大多数都是基于安卓系统运行的。...
安卓系统应用正在运行,实时监控... 你有没有发现,手机里那些安卓系统应用总是在默默无闻地运行着?它们就像一群勤劳的小蜜蜂,在你不知不觉中...
ios系统为什么比安卓系统流畅... 你有没有发现,同样是智能手机,iPhone的iOS系统总是比安卓系统运行得更加流畅呢?这其中的奥秘,...
不是安卓系统的有哪些,多样化的... 你有没有想过,手机的世界里,除了安卓系统,还有哪些操作系统在默默发光呢?今天,就让我带你一起探索那些...
读写安卓系统软件,深度解析与应... 你有没有想过,你的安卓手机里那些神奇的软件是怎么来的?今天,就让我带你一探究竟,揭开读写安卓系统软件...