场景用处:很多报表数据查询除了查指定某月某天到某月某天之外,还有就是查前一个月、前3个月、前半年等
1、获取一个月前的今天
- // 获取一个月前的今天
- getLastOneMonth(){
- var returnDate,
- date = new Date(),
- year = date.getFullYear(),
- month = date.getMonth() + 1, // 时间的月份是从0~11
- day = date.getDate()
- if(parseInt(month)<10){
- month = '0'+month;
- }
- if(parseInt(day)<10){
- day = '0'+day;
- }
- returnDate = year + '-' + (parseInt(month) - 1) + '-' + day
- //需要考虑特殊情况:
- //1、当前为1月,上个月就是去年的12月了
- //2、当天是31号,上个月没有31号
- if(parseInt(month) <= 1){ //month等于1,上个月就是去年12月
- year = parseInt(year) - 1;
- var prevSize = new Date(year,((12 - 1) + parseInt(month)),0).getDate; //获取上个月的总天数
- if(prevSize < parseInt(day)){
- // 其实这一步是不存在的,因为12月的总天数是等于1月的总天数的,只是需要考虑这种情况
- }
- returnDate = year + '-12-' + day;//即:year + '-' + ((12 - 1) + parseInt(month)) + '-' + day;
- }else{
- var prevSize = new Date(year,parseInt(month) - 1,0).getDate; //获取上个月的总天数
- if(prevSize < parseInt(day)){
- returnDate = year + '-' + month + '-01'
- }
- }
- return returnDate;
- }
2、获取三个月前的今天
- // 获取三个月前的今天
- getLastThreeMonth(){
- var returnDate,
- date = new Date(),
- year = date.getFullYear(),
- month = date.getMonth() + 1, // 时间的月份是从0~11
- day = date.getDate()
- if(parseInt(month)<10){
- month = '0'+month;
- }
- if(parseInt(day)<10){
- day = '0'+day;
- }
- returnDate = year + '-' + (parseInt(month) - 3) + '-' + day
- //需要考虑特殊情况:
- //1、当前月份小于等于3月,三个月就是属于去年的了
- //2、当天是31号,三个月前的今天没有31号
- if(parseInt(month) <= 3){ //month等于1,上个月就是去年12月
- year = parseInt(year) - 1;
- returnDate = year + '-' + (9 + parseInt(month)) + day;
- var prevSize = new Date(year,((12 - 3) + parseInt(month)),0).getDate; //获取上个月的总天数
- if(prevSize < parseInt(day)){
- returnDate = year + '-' + (12 - 3 + 1) + parseInt(month) + '-01'
- }
- }else{
- var prevSize = new Date(year,parseInt(month) - 1,0).getDate; //获取上个月的总天数
- if(prevSize < parseInt(day)){
- returnDate = year + '-' + (parseInt(month) - 2) + '-01'
- }
- }
- return returnDate;
- }
3、根据上面两个方法可以看得到,都是类似的,可以封装成一个函数
- // 获取n个月前的今天 n是有范围限制的,最多只能到去年1月,更久之前就不行了 n <= (当前月数+12)
- getLastMonth(n){
- var returnDate,
- date = new Date(),
- year = date.getFullYear(),
- month = date.getMonth() + 1, // 时间的月份是从0~11
- day = date.getDate()
- if(parseInt(month)<10){
- month = '0'+month;
- }
- if(parseInt(day)<10){
- day = '0'+day;
- }
- returnDate = year + '-' + (parseInt(month) - n) + '-' + day
- //需要考虑特殊情况:
- //1、当前月份小于等于n月,n个月前就是属于上一年的了
- //2、当天是31号,n个月前的今天有没有31号
- if(parseInt(month) <= n){ //month等于n,n个月就是属于去年
- year = parseInt(year) - 1;
- returnDate = year + '-' + ((12 - n) + parseInt(month)) + '-' + day;
- var prevSize = new Date(year,((12 - n) + parseInt(month)),0).getDate; //获取n个月前的总天数
- if(prevSize < parseInt(day)){
- returnDate = year + '-' + (12 - n + 1) + parseInt(month) + '-01'
- }
- }else{
- var prevSize = new Date(year,parseInt(month) - n,0).getDate; //获取n个月前的总天数
- if(prevSize < parseInt(day)){
- returnDate = year + '-' + (parseInt(month) - (n-1)) + '-01'
- }
- }
- return returnDate;
- }
***如果错误,欢迎各位指出,谢谢***
tips: 这种情况moment.js应该也有实现,本人没去试过