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("-")] }
联系站长:

相关文章
Animate.css使用方法,及源码下载,包含中文文档教程-css3动画演示合集,可用于uniapp
Animate.css是一个即用型跨浏览器动画库,可在您的 Web 项目中使用。非常适合强调、主页、滑块和注意力引导提示。注意:Animate.css可以在uniapp中使用,并且兼容多端,源码放页面...
js实用函数之map()函数的使用,一分钟学会
重点:map() 方法定义在JavaScript的Array中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。值得注意的是:1、map()函数不会对空数组进行检测;2、map...
js获取m到n随机数,js获取随机整数,从0到10,从m到n任意数
˂a class="reference-link" name="js获取从m到n的随机数"˃js获取从m到n的随机数function getRandomNumber(min, ma...
fingerprintjs2使用方法,以及fingerprintjs2文件下载
fingerprintjs2使用方法,以及fingerprintjs2文件下载获取唯一标识,如果有技术问题可以联系站长官方教程:<script> // ...
uniapp常用api整合
每次使用uniapp开发都有一些常用的api需要调用,直接写官方的api显的代码很杂乱,重复的太多,于是把这些常用的api整理起来,作为常用的工具挂载到vue对象上,这样在任何页面都可以用this获取...
php根据id和pid把单级数组重新组合为树结构
function list_to_trees($list, $pk='id', $pid = 'pid', $child =&nb...
发表评论
