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

相关文章

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

Mysql表如何按照日期字段的年月分区

《Mysql表如何按照日期字段的年月分区》:本文主要介绍Mysql表如何按照日期字段的年月分区的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、创键表时直接设置分区二、已有表分区1、分区的前置条件2、分区操作三、验证四、注意总结一、创键表时直接设置分区

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法

《golang获取当前时间、时间戳和时间字符串及它们之间的相互转换方法》:本文主要介绍golang获取当前时间、时间戳和时间字符串及它们之间的相互转换,本文通过实例代码给大家介绍的非常详细,感兴趣... 目录1、获取当前时间2、获取当前时间戳3、获取当前时间的字符串格式4、它们之间的相互转化上篇文章给大家介