需求要求,本月,上月,本周,上周,本季度,上季度,今年,去年,的开始时间,和结束时间

我封装了时间组件,满足不同的要求

//主要目的为了解决首页里面的开始日期和结束日期,用的是element-ui的组件el-date-picker
//因为需求又本月,上月,本周,上周,本季度,上季度,等一些区分。

//首页的工作统计用的是默认显示上个月的1号到最后一天,只有截图,后面复制过来规则。
//默认显示上月的1号,就是开始时间

//默认显示上个月的1号具体日期
export function DefaultDisplayLastMonthStart() {
    const start = new Date();  //当前的时间
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth(); //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()   //当前日期号
    let Month2 = ""
    let Year2 = ""
    let day2 = ""
    if (Month == '0') {  //这里需求是要上个月的日期,所有判断当前月是否1月,为1月就要取12月,取上一年
        Month2 = '12'
        Year2 = Year - 1 
    }else {              //如果不是1月,就是取Month不用加1了,年份就是当前年份
        Month2 = Month
        Year2 = Year
    }
    //给月份补零
    if (Month2 < 10) {
        Month2 = "0" + Month2
    }
    let startPlaceholder = ""
    startPlaceholder = Year2 + "-" + Month2 + "-" + '01';
    return startPlaceholder
}

//默认显示上月的最后一天具体日期,就是结束时间
export function DefaultDisplayLastMonthEnd() {
    const start = new Date();  //当前的时间
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth(); //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()   //当前日期号
    let Month2 = ""
    let Year2 = ""
    let day2 = ""
    if (Month == '0') {  //这里需求是要上个月的日期,所有判断当前月是否1月,为1月就要取12月,取上一年
        Month2 = '12'
        Year2 = Year - 1 
    }else {              //如果不是1月,就是取Month不用加1了,年份就是当前年份
        Month2 = Month
        Year2 = Year
    }
    //获取当年2月的天数
    var dayNum = new Date(Year2,2,0).getDate();
    console.log(dayNum)      
    //判断月份获取天数
    if (Month2 == "1") {
        day2 = 31
    }
    if (Month2 == "2") {
        day2 = dayNum
    }
    if (Month2 == "3") {
        day2 = 31
    }
    if (Month2 == "4") {
        day2 = 30
    }
    if (Month2 == "5") {
        day2 = 31
    }
    if (Month2 == "6") {
        day2 = 30
    }
    if (Month2 == "7") {
        day2 = 31
    }
    if (Month2 == "8") {
        day2 = 31
    }
    if (Month2 == "9") {
        day2 = 30
    }
    if (Month2 == "10") {
        day2 = 31
    }
    if (Month2 == "11") {
        day2 = 30
    }
    if (Month2 == "12") {
        day2 = 31
    }
    //给月份补零
    if (Month2 < 10) {
        Month2 = "0" + Month2
    }
    // //给日期补零
    // if (day2 < 10) {
    //     Month2 = "0" + Month2
    // }
    let startPlaceholder = ""
    startPlaceholder = Year2 + "-" + Month2 + "-" + day2;
    return startPlaceholder
}
//计算从当前时间到上个季度开始的天数,比如今天是1.07,那就是10月+11月+12月+7天,就等于10月1号

//上个季度开始时间 要减去的天数
export function LastQuarterStart() {
    let day2 = ""
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month2 = new Date().getMonth() + 1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()-1   //当前日期号
    console.log(Month2)
    if (Month2 == "1" || Month2 == "2" || Month2 == "3") { //取10,11,12月加当前的天数-1
        day2 = 31 + 30 + 31 + day
        console.log(day2,"day2")
    }
    if (Month2 == "4" || Month2 == "5" || Month2 == "6") { //取1,2,3月加当前的天数-1
        //获取当年2月的天数
        var dayNum = new Date(Year,2,0).getDate();
        day2 = 31 + dayNum + 31 + day
        console.log(day2,"day2")
    }
    if (Month2 == "7" || Month2 == "8" || Month2 == "9") { //取4,5,6月加当前的天数-1
        day2 = 30 + 31 + 30 + day
        console.log(day2,"day2")
    }
    if (Month2 == "10" || Month2 == "11" || Month2 == "12") { //取7,8,9月加当前的天数-1
        day2 = 31 + 31 + 30 + day
        console.log(day2,"day2")
    }
    return day2
}
//计算从当前时间到上个季度结束的天数,比如今天是2.07,那就是1月+7天,就等于12月31号

//上个季度结束时间要减去的天数
export function LastQuarterend() {
    let day2 = ""
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month2 = new Date().getMonth() + 1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()  //当前日期号
    console.log(Month2)
    if (Month2 == "1" || Month2 == "4" || Month2 == "7" || Month2 == "10") { //当前的天数即可
        day2 = day
        console.log(day2,"day2")
    }
    if (Month2 == "2" || Month2 == "8" || Month2 == "11") { //如果是2,7月,就是当前加上1,7月份天数
        day2 = 31 + day
    }
    if (Month2 == "5") { //如果是5月,就是当前加上4月份天数
        day2 = 30 + day
    }
    if (Month2 == "3") { //如果是3月,就是当前加上1,2月份天数
        //获取当年2月的天数
        var dayNum = new Date(Year,2,0).getDate();
        day2 = 31 + dayNum + day
    }
    if (Month2 == "6" || Month2 == "12") { //如果是6月,就是当前加上4,5月份天数
        day2 = 30 + 31 + day
    }
    if (Month2 == "9") { //如果是9月,就是当前加上7,8月份天数
        day2 = 31 + 31 + day
    }
    return day2
}

//首页的点评任务的开始时间

//点评任务的结束时间需要当前时间

//默认显示当前时间的具体日期
export function nowTime () {
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth() + 1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()   //当前日期号
    //给月份补零
    if (Month < 10) {
        Month = "0" + Month
    }
    //给日期补零
    if (day < 10) {
        day = "0" + day
    }
    let ThisMonth = Year + "-" + Month + "-" + day;
    return ThisMonth
}

//默认显示本月的1号,就是开始时间

export function ThisMonth() {
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth() + 1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()   //当前日期号
    //给月份补零
    if (Month < 10) {
        Month = "0" + Month
    }
    let ThisMonth = Year + "-" + Month + "-" + '01';
    console.log(ThisMonth)
    return ThisMonth
}

//本周就是当前周的周一到当前时间,要减去的天数
export function ThisWeekh() {
    let dayWeek = new Date().getDay() - 1; //获取当前星期X(0-6,0代表星期天)
    console.log(dayWeek)  //如果是周四,就是4
    return dayWeek
}

//本月就是当前月份的1号开始时间,要减去的天数
export function ThisMonth2() {
    let day = new Date().getDate() - 1   //当前日期号
    return day
}

//本季度开始时间就是当前季度的第一个月份1号,要减去的天数
export function ThisQuarterStart() {
    let day2 = ""
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month2 = new Date().getMonth() + 1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()-1   //当前日期号
    if (Month2 == "1" || Month2 == "4" || Month2 == "7" || Month2 == "10") { //当前的天数即可
        day2 = day
        console.log(day2,"day2")
    }
    if (Month2 == "2" || Month2 == "8" || Month2 == "11") { //如果是2,7月,就是当前加上1,7月份天数
        day2 = 31 + day
    }
    if (Month2 == "5") { //如果是5月,就是当前加上4月份天数
        day2 = 30 + day
    }
    if (Month2 == "3") { //如果是3月,就是当前加上1,2月份天数
        //获取当年2月的天数
        var dayNum = new Date(Year,2,0).getDate();
        day2 = 31 + dayNum + day
    }
    if (Month2 == "6" || Month2 == "12") { //如果是6月,就是当前加上4,5月份天数
        day2 = 30 + 31 + day
    }
    if (Month2 == "9") { //如果是9月,就是当前加上7,8月份天数
        day2 = 31 + 31 + day
    }
    return day2
}

//首页的点评结果统计
//默认显示近一年的开始时间和结束时间,指当前月份之前的12个自然月,

//近一年的开始时间,具体日期
export function NearlyYearStart() {
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth() + 1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()   //当前日期号
    //给月份补零
    if (Month == '1') {
        Month = Month
    }else {
        Month = Month - 1 
    }
    if (Month < 10) {
        Month = "0" + Month
    }
    // //给日期补零
    // if (day < 10) {
    //     day = "0" + day
    // }
    let ThisMonth = (Year - 1) + "-" + Month + "-" + '01';
    return ThisMonth
}
//近一年的结束时间,具体日期
export function NearlyYearEnd() {
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth() + 1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()   //当前日期号
    let day2 = "";
    if (Month == '1') {
        Month = '12'
        Year = Year -1
    }else {
        Month = Month - 1 
        Year = Year
    }
    //给月份补零
    if (Month < 10) {
        Month = "0" + Month
    }
    //获取当年2月的天数
    var dayNum = new Date(Year,2,0).getDate();
    //判断月份获取天数
    if (Month == "1") {
        day2 = 31
    }
    if (Month == "2") {
        day2 = dayNum
    }
    if (Month == "3") {
        day2 = 31
    }
    if (Month == "4") {
        day2 = 30
    }
    if (Month == "5") {
        day2 = 31
    }
    if (Month == "6") {
        day2 = 30
    }
    if (Month == "7") {
        day2 = 31
    }
    if (Month == "8") {
        day2 = 31
    }
    if (Month == "9") {
        day2 = 30
    }
    if (Month == "10") {
        day2 = 31
    }
    if (Month == "11") {
        day2 = 30
    }
    if (Month == "12") {
        day2 = 31
    }
    
    let ThisMonth = Year + "-" + Month + "-" + day2;
    return ThisMonth
}
//上个月的开始时间,要减去的天数,用的element-ui组件,所以计算的是那当前时间要减去的天数,如当前1月8号,那就是减去12月的31天加这个月的8天
export function lastMonthStart() {
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth()+1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()-1   //当前日期号
    let day2 = "";
    //获取当年2月的天数
    var dayNum = new Date(Year,2,0).getDate();
    if (Month == "1") {  //如果当前是1月,就当前的日期加上12月的天数
        day2 = day + 31
    }
    if (Month == "2") {
        day2 = day + 31
    }
    if (Month == "3") {
        day2 = day + dayNum
    }
    if (Month == "4") {
        day2 = day + 31
    }
    if (Month == "5") {
        day2 = day + 30
    }
    if (Month == "6") {
        day2 = day + 31
    }
    if (Month == "7") {
        day2 = day + 30
    }
    if (Month == "8") {
        day2 = day + 31
    }
    if (Month == "9") {
        day2 = day + 31
    }
    if (Month == "10") {
        day2 = day + 30
    }
    if (Month == "11") {
        day2 = day + 31
    }
    if (Month == "12") {
        day2 = day + 30
    }
    return day2
}
//上个月的结束时间,要减去的天数,比如当前是1.8号,上个月结束时间就是12.31号,就是减去当前的天数即可
export function lastMonthEnd() {
    let day = new Date().getDate()   //当前日期号
    return day;
}

//近一年的的开始日期,要减去的天数, 近一年是指当前月之前的12个自然月,比如当前是1月,就是前一年的1月1号到12月31号
export function NearlyYearStart2() {
    let Year = new Date().getFullYear() -1; //获取完整的年份(4位)
    let Year2 = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth()+1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate() -1   //当前日期号
    let day2 = "";
    //获取上一年2月的天数
    var dayNum = new Date(Year,2,0).getDate();
    //获取当年2月的天数
    var dayNum2 = new Date(Year2,2,0).getDate();
    if (Month == "1" || Month == "2") { //如果当前是2月,就是取2.1号-1.31号
        day2 = 31 + dayNum + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + day;
    }if (Month == "3" || Month == "4" || Month == "5" || Month == "6" || Month == "7" || Month == "8" || Month == "9" || Month == "10" || Month == "11"|| Month == "12") {
        day2 = 31 + dayNum2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + day;
    }
    console.log(day2)
    return day2;
}
//近一年的的结束日期,要减去的天数, 近一年是指当前月之前的12个自然月,比如当前是1月,就是前一年的1月1号到12月31号
export function NearlyYearEnd2() {
    let day = new Date().getDate()   //当前日期号
    return day;
}

//今年的开始时间,减去的天数
export function NowYearStart() {
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth()+1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate() -1  //当前日期号
    let day2 = "";
    var dayNum = new Date(Year,2,0).getDate();
    if (Month == "1") {  
        day2 = day 
    }
    if (Month == "2") {
        day2 = day + 31
    }
    if (Month == "3") {
        day2 = day + dayNum + 31
    }
    if (Month == "4") {
        day2 = day + 31 + dayNum + 31
    }
    if (Month == "5") {
        day2 = day + 30 + 31 + dayNum + 31
    }
    if (Month == "6") {
        day2 = day + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "7") {
        day2 = day + 30 + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "8") {
        day2 = day + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "9") {
        day2 = day + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "10") {
        day2 = day + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "11") {
        day2 = day + 31 + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "12") {
        day2 = day + 30 + 31 + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    return day2;
}

//去年的开始时间,减去的天数  去年是指当前年份的前一年的1月1号到12.31号
export function lastYearStart() {
    let Year = new Date().getFullYear()-1; //获取完整的年份(4位)
    let Month = new Date().getMonth()+1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()  //当前日期号
    let day2 = "";
    //算前一年的2月天数
    var dayNum = new Date(Year,2,0).getDate();
    //去年的天数总和
    let lastYearDay = 31 + dayNum + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 30 + 30 + 31;  
    if (Month == "1") {
        day2 = lastYearDay + day;
    }
    if (Month == "2") {
        day2 = lastYearDay + day + 31
    }
    if (Month == "3") {
        day2 = lastYearDay + day + dayNum + 31
    }
    if (Month == "4") {
        day2 = lastYearDay + day + 31 + dayNum + 31
    }
    if (Month == "5") {
        day2 = lastYearDay + day + 30 + 31 + dayNum + 31
    }
    if (Month == "6") {
        day2 = lastYearDay + day + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "7") {
        day2 = lastYearDay + day + 30 + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "8") {
        day2 = lastYearDay + day + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "9") {
        day2 = lastYearDay + day + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "10") {
        day2 = lastYearDay + day + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "11") {
        day2 = lastYearDay + day + 31 + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "12") {
        day2 = lastYearDay + day + 30 + 31 + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    return day2;

}

//去年的结束时间,减去的天数  去年是指当前年份的前一年的1月1号到12.31号
export function lastYearEnd() {
    let Year = new Date().getFullYear(); //获取完整的年份(4位)
    let Month = new Date().getMonth()+1; //获取当前月份(0-11,0代表1月)要加1
    let day = new Date().getDate()  //当前日期号
    let day2 = "";
    //算今年的2月天数
    var dayNum = new Date(Year,2,0).getDate();
    if (Month == "1") {  
        day2 = day 
    }
    if (Month == "2") {
        day2 = day + 31
    }
    if (Month == "3") {
        day2 = day + dayNum + 31
    }
    if (Month == "4") {
        day2 = day + 31 + dayNum + 31
    }
    if (Month == "5") {
        day2 = day + 30 + 31 + dayNum + 31
    }
    if (Month == "6") {
        day2 = day + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "7") {
        day2 = day + 30 + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "8") {
        day2 = day + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "9") {
        day2 = day + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "10") {
        day2 = day + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "11") {
        day2 = day + 31 + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    if (Month == "12") {
        day2 = day + 30 + 31 + 30 + 31 + 31 + 30  + 31 + 30 + 31 + dayNum + 31
    }
    return day2;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值