温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

js获取最近一周一个月三个月时间的示例分析

发布时间:2021-12-28 13:06:14 来源:亿速云 阅读:150 作者:柒染 栏目:开发技术

js获取最近一周一个月三个月时间的示例分析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

获取近一周时间

var end = new Date();
var year = end.getFullYear();
var month = end.getMonth() + 1;//0-11表示1-12月
var day = end.getDate();
var dateObj = {};
dateObj.end = year + '-' + month + '-' + day;
if (day - 7 <= 0) {   //如果在当月7日之前
    var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();    //1周前所在月的总天数
    if (month - 1 <= 0) { //如果在当年的1月份
      dateObj.start = (year - 1) + '-' + 12 + '-' + (31 - (7 - day));
    } else {
      dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (7 - day));
    }
} else {
    dateObj.start = year + '-' + month + '-' + (day - 7);
}
console.log(JSON.stringify(dateObj))
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

获取近一个月时间

var end = new Date();
var year = end.getFullYear();
var month = end.getMonth() + 1;//0-11表示1-12月
var day = end.getDate();
var dateObj = {};
dateObj.end = year + '-' + month + '-' + day;
var endMonthDay = new Date(year, month, 0).getDate();    //当前月的总天数
if(month - 1 <= 0){ //如果是1月,年数往前推一年<br>    
    dateObj.start = (year - 1) + '-' + 12 + '-' + day;
}else{
    var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();
    if(startMonthDay < day){    //1个月前所在月的总天数小于现在的天日期
        if(day < endMonthDay){        //当前天日期小于当前月总天数
            dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day));
        }else{
            dateObj.start = year + '-' + (month - 1) + '-' + startMonthDay;
        }
    }else{
        dateObj.start = year + '-' + (month - 1) + '-' + day;
    }
}
console.log(JSON.stringify(dateObj))
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.

获取近三个月时间

var end = new Date();
var year = end.getFullYear();
var month = end.getMonth() + 1;//0-11表示1-12月
var day = end.getDate();
var dateObj = {};
dateObj.end = year + '-' + month + '-' + day;
var endMonthDay = new Date(year, month, 0).getDate();    //当前月的总天数
if(month - 3 <= 0){ //如果是1、2、3月,年数往前推一年
    var start3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate();    //3个月前所在月的总天数
    if(start3MonthDay < day){    //3个月前所在月的总天数小于现在的天日期
        dateObj.start = (year - 1) + '-' + (12 - (3 - month)) + '-' + start3MonthDay;
    }else{
        dateObj.start = (year - 1) + '-' + (12 - (3 - month)) + '-' + day;
    }
}else{
    var start3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate();    //3个月前所在月的总天数
    if(start3MonthDay < day){    //3个月前所在月的总天数小于现在的天日期
        if(day < endMonthDay){        //当前天日期小于当前月总天数,2月份比较特殊的月份
            dateObj.start = year + '-' + (month - 3) + '-' + (start3MonthDay - (endMonthDay - day));
        }else{
            dateObj.start = year + '-' + (month - 3) + '-' + start3MonthDay;
        }
    }else{
        dateObj.start = year + '-' + (month - 3) + '-' + day;
    }
}
console.log(JSON.stringify(dateObj))

New Date()与setDate()参数

相信网上已经有很多关于日期的文章了,这里只是我自己再工作中遇到的问题然后加以总结;

new Date()

new Date() 一共有六种形式,五种带参数的一种不带参数的;

  1. new Date();自然不用多说,默认获取的是当前日期。

  2. new Date("month2 dd,yyyy hh:mm:ss"); 注意:参数是字符形式

  3. new Date("month2 dd,yyyy"); 注意:参数是字符形式

  4. new Date(yyyy,month3,dd,hh,mm,ss); 注意:参数不是字符

  5. new Date(yyyy,month3,dd); 注意:参数不是字符

  6. new Date(ms); 

参数说明:

month2:用英文,表示月份名称;从January到December ;

dd:表示日期,1-31

yyyy:表示四位表示的年份

hh:mm:ss:表示时间,时(0-23)-分(0-59)-秒(0-59)

month3:是Number型的月份;从0-11;即1月到12月

ms:从1970年1月1日之间相差的毫秒数

特别提醒:有些是字符形式有些不是

关于js获取最近一周一个月三个月时间的示例分析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI