js获取本月,本年,近一月,近3月,近1年的日期范围,获取近n月的日期范围

js获取本月,本年,近一月,近3月,近1年的日期范围,获取近n月的日期范围
function getnMonth(i) { console.log("获取近i月的时间段", i) var now = new Date() var year = now.getFullYear() var month = now.getMonth() + 1; var newmonth=month < 10 ? "0" + month : month var day = now.getDate() var dateObj = {} dateObj.endTime = year + "-" + newmonth + "-" + day var nowMonthDay = new Date(year, month, 0).getDate() //当前月的总天数 if (i == 12) { //如果是12月,年数往前推一年<br> dateObj.startTime = year - 1 + "-" + newmonth + "-" + day } else if (month - i <= 0) { // 如果前推i月小于0,年数往前推一年<br> dateObj.startTime = year - 1 + "-" + (12+month-i) + "-" + day } else { var endTimeMonthDay = new Date(year, parseInt(month) - i, 0).getDate() if (endTimeMonthDay < day) { // i个月前所在月的总天数小于现在的天日期 let mon=month - i; mon1=mon < 10 ? "0" + mon : mon if (day < nowMonthDay) { // 当前天日期小于当前月总天数 dateObj.startTime = year + "-" + mon1 + "-" + (endTimeMonthDay - (nowMonthDay - day)) } else { dateObj.startTime = year + "-" +mon1+ "-" + endTimeMonthDay } } else { dateObj.startTime = year + "-" + mon1 + "-" + day } } return [dateObj.startTime,dateObj.endTime] } getnMonth(3)//获取近3月时间段 //['2023-11-23', '2024-01-23']
获取当月、当年时间范围:
/** * 当月 * @param {Date} date * @returns {String} 当月 */ export function getCurmonth(date) { const time = date ? new Date(date) : new Date() const y = time.getFullYear() let m = time.getMonth() + 1 let d = time.getDate() m = m < 10 ? "0" + m : m const d1 = "01" return [[y, m, d1].join("-"),[y, m, d].join("-")] } /** * 当年 * @param {Date} date * @returns {String} 当年 */ export function getCuryear(date) { const time = date ? new Date(date) : new Date() const y = time.getFullYear() let m = "1"; let m1 = time.getMonth() + 1 let d = time.getDate() m = m < 10 ? "0" + m : m const d1 = "01" return [[y, m, d1].join("-"),[y, m, d].join("-")] }
联系站长:

相关文章
uniapp常用api整合
每次使用uniapp开发都有一些常用的api需要调用,直接写官方的api显的代码很杂乱,重复的太多,于是把这些常用的api整理起来,作为常用的工具挂载到vue对象上,这样在任何页面都可以用this获取...
【JavaScript】js获取当前年月日周
【JavaScript】js获取当前年月日周function getcurtime(){ const addZero =...
Animate.css使用方法,及源码下载,包含中文文档教程-css3动画演示合集,可用于uniapp
Animate.css是一个即用型跨浏览器动画库,可在您的 Web 项目中使用。非常适合强调、主页、滑块和注意力引导提示。注意:Animate.css可以在uniapp中使用,并且兼容多端,源码放页面...
原生js把时间戳转为日期格式年月日时分秒
原生js把时间戳转为日期格式年月日时分秒function parsetime(timestamp){ let timelength...
js使用正则表达式获取html字符串中的img标签的src组成数组
js使用正则表达式获取html字符串中的img标签的src组成数组exec() 方法用于检索字符串中的正则表达式的匹配。如果字符串中有匹配的值返回该匹配值,否则返回 null。var htm...
fingerprintjs2使用方法,以及fingerprintjs2文件下载
fingerprintjs2使用方法,以及fingerprintjs2文件下载获取唯一标识,如果有技术问题可以联系站长官方教程:<script> // ...
发表评论
