首页 > 其他分享 >Moment js获取今日、昨天、这周、上周、本月、上月方法

Moment js获取今日、昨天、这周、上周、本月、上月方法

时间:2022-12-30 20:35:40浏览次数:37  
标签:end format DD moment js YYYY 获取 Moment start

import moment from "moment"

//日期类型
const DATE_CATEGORY = {
    today: 'today',
    yesterday: 'yesterday',
    week: 'week',
    lastWeek: 'lastWeek',
    month: 'month',
    lastMonth: 'lastMonth',
}

//开始时间
let start = ''
//结束时间
let end = ''

let handleDateType = {
    //今日
    today: (() => {
        start = moment().format("YYYY-MM-DD")
        end = moment().format("YYYY-MM-DD")
        return {start , end}
    })(),
    //昨日
    yesterday: (() => {
        start = moment().subtract(1, 'days').format('YYYY-MM-DD');
        end = moment().subtract(1, 'days').format('YYYY-MM-DD');
        return {start , end}
    })(),
    //这周
    week: (() => {
        start = moment().week(moment().week()).startOf('week').format('YYYY-MM-DD');
        end = moment().week(moment().week()).endOf('week').format('YYYY-MM-DD');
        return {start , end}
    })(),
    //上周
    lastWeek: (() => {
        let weekOfday = parseInt(moment().format('d'))
        start = moment().subtract(weekOfday + 7, 'days').format('YYYY-MM-DD') 
        end = moment().subtract(weekOfday + 1, 'days').format('YYYY-MM-DD')
        return {start , end}
    })(),
    //本月
    month: (() => {
        start = moment().startOf('month').format('YYYY-MM-DD'); 
        end = moment().endOf('month').format('YYYY-MM-DD'); 
        return {start , end}
    })(),
    //上月
    lastMonth: (() => {
        start = moment().subtract(1,'month').startOf('month').format('YYYY-MM-DD')
        end = moment().subtract(1,'month').endOf('month').format('YYYY-MM-DD')
        return {start , end}
    })()
}

export default {
    DATE_CATEGORY,
    handleDateType
}


标签:end,format,DD,moment,js,YYYY,获取,Moment,start
From: https://www.cnblogs.com/ianlab/p/17015746.html

相关文章