JS快速获取日期时间(昨天今天明天后天、上下月份、年份的第一天和最后一天)

本文主要是介绍JS快速获取日期时间(昨天今天明天后天、上下月份、年份的第一天和最后一天),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

获取当前日期或任意日期的昨天今天明天后天、上下月份、年份的第一天和最后一天
在这里插入图片描述

   // 这里模拟的当天日期是2008-08-08console.log("昨天:" + this.getDateTime(-1));console.log("今天:" + this.getDateTime(0));console.log("明天:" + this.getDateTime(1));console.log("后天:" + this.getDateTime(2));console.log("指定日期的第二天(不补0):" + this.getDateTime(1, "yyyy-M-d", "2008-06-06"));console.log("今天日期+时间:(修改分隔符)" + this.getDateTime(0, "yyyy/MM/dd HH-mm-ss"));console.log("今天日期+时间:" + this.getDateTime(0, "yyyy-MM-dd HH:mm"));console.log("今天日期(不包含年):" + this.getDateTime(0, "MM-dd"));console.log("今天日期+时间(包含秒):" + this.getDateTime(0, "yyyy-MM-dd HH:mm:ss"));console.log("今天日期+时间(包含星期):" + this.getDateTime(0, "yyyy-MM-dd HH:mm:ss week"));console.log("当月第一天:" + this.getDateTime("month"));console.log("当月最后一天:" + this.getDateTime("monthEnd"));console.log("上个月第一天:" + this.getDateTime("beforeMonth"));console.log("上个月最后一天:" + this.getDateTime("beforeMonthEnd"));console.log("下个月第一天:" + this.getDateTime("afterMonth"));console.log("下个月最后一天:" + this.getDateTime("afterMonthEnd"));console.log("当年第一天:" + this.getDateTime("year"));console.log("当年最后一天:" + this.getDateTime("yearEnd"));console.log("上年第一天:" + this.getDateTime("beforeYear"));console.log("上年最后一天:" + this.getDateTime("beforeYearEnd"));console.log("下年第一天:" + this.getDateTime("afterYear"));console.log("下年最后一天:" + this.getDateTime("afterYearEnd"));
       /*** @description:* @param {*} type 类型 默认今天* @param {*} config 格式 yyyy-MM-dd HH:mm:ss week 年-月-日 时:分:秒 星期* @param {*} setDate 指定日期* @return {*}*/getDateTime(type = 0, config = "yyyy-MM-dd", setDate) {let sep = config.match(/[^a-zA-Z0-9]/g); //获取字符串中的符号(即非字母和非数字的字符)sep = Array.from(new Set(sep)); //去重let strLenObj = [...config].reduce((a, b) => (a[b] ? a[b]++ : (a[b] = 1), a), {});let thisDate = new Date();if (setDate) thisDate = new Date(setDate); //设置指定日期switch (type) {case "month": //当月第一天thisDate.setDate(1);break;case "monthEnd": //当月最后一天thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() + 1, 0);break;case "beforeMonth": //上个月第一天thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() - 1, 1);break;case "beforeMonthEnd": //上个月最后一天thisDate = new Date(new Date(thisDate.getFullYear(), thisDate.getMonth(), 1) - 1);break;case "afterMonth": //下个月第一天thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() + 1, 1);break;case "afterMonthEnd": //下个月最后一天thisDate = new Date(thisDate.getFullYear(), thisDate.getMonth() + 2, 0);break;case "year":thisDate.setDate(1); //当年第一天thisDate.setMonth(0);break;case "yearEnd": //当年最后一天thisDate.setMonth(12);thisDate.setDate(0);break;case "beforeYear": //上年第一天thisDate = new Date(thisDate.getFullYear() - 1, 0, 1);break;case "beforeYearEnd": //上年最后一天thisDate = new Date(thisDate.getFullYear(), 0, 0);break;case "afterYear": //下年第一天thisDate = new Date(thisDate.getFullYear() + 1, 0, 1);break;case "afterYearEnd": //下年最后一天thisDate = new Date(thisDate.getFullYear() + 2, 0, 0);break;default:thisDate.setDate(thisDate.getDate() + type);break;}const year = thisDate.getFullYear();const month = thisDate.getMonth() + 1;const day = thisDate.getDate();const hours = new Date().getHours();const minutes = new Date().getMinutes();const seconds = new Date().getSeconds();// 获取星期const weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];const dayOfWeek = weekdays[thisDate.getDay()];let result = "";let hasYear = typeof type == "string" && type.toLowerCase().includes("year"); //如果是字符串 判断是佛有年yearif (strLenObj.y || hasYear) {result += year;}if (strLenObj.M) {result += (strLenObj.y || hasYear ? sep[0] : "") + (month < 10 && strLenObj.M > 1 ? "0" + month : month);}if (strLenObj.d) {result += (strLenObj.M ? sep[0] : "") + (day < 10 && strLenObj.d > 1 ? "0" + day : day);}if (strLenObj.H) result += sep[1] + (hours < 10 && strLenObj.H > 1 ? "0" + hours : hours);if (strLenObj.m) result += sep[2] + (minutes < 10 && strLenObj.m > 1 ? "0" + minutes : minutes);if (strLenObj.s) result += sep[2] + (seconds < 10 && strLenObj.s > 1 ? "0" + seconds : seconds);if (config.toLowerCase().includes("week")) result += sep[1] + dayOfWeek; //包含星期return result;},

这篇关于JS快速获取日期时间(昨天今天明天后天、上下月份、年份的第一天和最后一天)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/766462

相关文章

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

快速修复一个Panic的Linux内核的技巧

《快速修复一个Panic的Linux内核的技巧》Linux系统中运行了不当的mkinitcpio操作导致内核文件不能正常工作,重启的时候,内核启动中止于Panic状态,该怎么解决这个问题呢?下面我们就... 感谢China编程(www.chinasem.cn)网友 鸢一雨音 的投稿写这篇文章是有原因的。为了配置完

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.