前端近一周近一个月近三个月近半年近一年js代码

// 近一周

export function getOneWeek() {

    var now = new Date();

    var year = now.getFullYear();

    var month = now.getMonth() + 1;//0-11表示1-12月

    var day = now.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    if (day - 7 <= 0) {   //如果在当月7日之前

        var lastMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();    //1周前所在月的总天数

        if (month - 1 <= 0) { //如果在当年的1月份

            dateObj.last = (year - 1) + '-' + 12 + '-' + (31 - (7 - day));

        } else {

            dateObj.last = year + '-' + (month - 1) + '-' + (lastMonthDay - (7 - day));

        }

    } else {

        dateObj.last = year + '-' + month + '-' + (day - 7);

    }

    return dateObj;

}

// 近一个月

export function getOneMonth() {

    var end = new Date();

    var year = end.getFullYear();

    var month = end.getMonth() + 1 < 10 ? `0${end.getMonth() + 1}` : end.getMonth() + 1

    var day = end.getDate() > 9 ? end.getDate() : '0' + end.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    var endMonthDay = new Date(year, month, 0).getDate();

    if (month - 1 <= 0) {

        dateObj.last = (year - 1) + '-' + 12 + '-' + day;

    } else {

        var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();

        if (startMonthDay < day) {

            if (day < endMonthDay) {

                dateObj.last = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day));

            } else {

                dateObj.last = year + '-' + (month - 1) + '-' + startMonthDay;

            }

        } else {

            dateObj.last = year + '-' + ((month - 1) < 10 ? `0${(month - 1)}` : (month - 1)) + '-' + day;

        }

    }

    return dateObj

}

// 近三个月

export function getThreeMounth() {

    var end = new Date();

    var year = end.getFullYear();

    var month = end.getMonth() + 1 < 10 ? `0${end.getMonth() + 1}` : end.getMonth() + 1

    var day = end.getDate() > 9 ? end.getDate() : '0' + end.getDate();

    var dateObj = {};

    dateObj.now = 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.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + start3MonthDay;

        } else {

            dateObj.last = (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.last = year + '-' + (month - 3) + '-' + (start3MonthDay - (endMonthDay - day));

            } else {

                dateObj.last = year + '-' + (month - 3) + '-' + start3MonthDay;

            }

        } else {

            dateObj.last = year + '-' + ((month - 3) < 10 ? `0${(month - 3)}` : (month - 3)) + '-' + day;

        }

    }

    return dateObj

}

// 近半年

export function getHalfYear() {

    var now = new Date();

    var year = now.getFullYear();

    var month = now.getMonth() + 1;//0-11表示1-12月

    var day = now.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    // 先获取当前时间

    var curDate = (new Date()).getTime()

    // 将半年的时间单位换算成毫秒

    var halfYear = 365 / 2 * 24 * 3600 * 1000

    // 半年前的时间(毫秒单位)

    var pastResult = curDate - halfYear

    // 日期函数,定义起点为半年前

    var pastDate = new Date(pastResult)

    var pastYear = pastDate.getFullYear()

    var pastMonth = pastDate.getMonth() + 1

    var pastDay = pastDate.getDate()

    if (pastMonth >= 1 && pastMonth <= 9) {

        pastMonth = '0' + pastMonth

    }

    if (pastDay >= 0 && pastDay <= 9) {

        pastDay = '0' + pastDay

    }

    dateObj.last = pastYear + '-' + pastMonth + '-' + pastDay

    return dateObj

}

// 近一年

export function getOneYear() {

    var nowDate = new Date()

    var dates = new Date(nowDate)

    var year = dates.getFullYear();

    var month = dates.getMonth() + 1;//0-11表示1-12月

    var day = dates.getDate();

    var dateObj = {};

    dateObj.now = year + '-' + month + '-' + day;

    dates.setDate(dates.getDate() - 365)

    var seperator1 = '-'

    var year = dates.getFullYear()

    var month = dates.getMonth() + 1

    var strDate = dates.getDate()

    if (month >= 1 && month <= 9) {

        month = '0' + month

    }

    if (strDate >= 0 && strDate <= 9) {

        strDate = '0' + strDate

    }

    dateObj.last = year + seperator1 + month + seperator1 + strDate

    return dateObj

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值