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("-")]
}联系站长:
相关文章
fingerprintjs2使用方法,以及fingerprintjs2文件下载
fingerprintjs2使用方法,以及fingerprintjs2文件下载获取唯一标识,如果有技术问题可以联系站长官方教程:<script> // ...
【JavaScript】js获取当前年月日周
【JavaScript】js获取当前年月日周function getcurtime(){ const addZero =...
jquery获取input选中值,jquery获取radio选中值,jquery获取select选中值,jquery获取checkbox选中值,jquery使用ajax异步提交表单数据
jquery获取input值://<input name="myinput"/> var inputValues = $(...
js实用函数之map()函数的使用,一分钟学会
重点:map() 方法定义在JavaScript的Array中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。值得注意的是:1、map()函数不会对空数组进行检测;2、map...
js使用正则表达式获取html字符串中的img标签的src组成数组
js使用正则表达式获取html字符串中的img标签的src组成数组exec() 方法用于检索字符串中的正则表达式的匹配。如果字符串中有匹配的值返回该匹配值,否则返回 null。var htm...
css显示三行文字,溢出显示省略号
css显示三行文字,溢出显示省略号(显示任意行,可以把数字3替换成其他数字):.text-wrap { display: -webkit-box; &n...





