moment.js 配合 elementUI 使用

本日/本周/本月/本季/本年比较简单
scope分别为/day/week/month/quarter/year

const startDate =moment().startOf(scope).valueOf();//.valueOf()获取到的是Long,不然就是一个Moment对象
const endDate = moment().valueOf();//获取的是当前系统时间

上周一00:00:00~周日23:59:59

 
const startDate = moment().week(moment().week() - 1).startOf('week').valueOf();
const endDate = moment().week(moment().week() - 1).endOf('week').valueOf();

上个月一号的00:00:00~上个月最后一天的23:59:59

 
const startDate = moment().month(moment().month() - 1).startOf('month').valueOf();
const endDate = moment().month(moment().month() - 1).endOf('month').valueOf();

上个季度第一个月一号的00:00:00~上个季度最后一个月最后一天的23:59:59

const startDate = moment().quarter(moment().quarter() - 1).startOf('quarter').valueOf();
const endDate = moment().quarter(moment().quarter() - 1).endOf('quarter').valueOf();

这里有一个需要注意的地方

为何每次都是声明一个常量/变量,是因为如果按以下代码写,你会发现你的startDate也变成了当天的结束时间。

 
let startDate = moment().startOf('day');
let endDate = startDate().endOf('day');

 

elementUI时间选择器的时间范围限定:


<el-date-picker :style="{width: '95%'}"
v-model="marketing.date"
type="date" value-format="yyyy-MM-dd" :picker-options="pickerOptions"
placeholder="选择日期">
</el-date-picker>

data():
pickerOptions: { disabledDate(time) {
// 本月初 const begin = moment().month(moment().month()).startOf('month').valueOf(); // 本月末 const end = moment().month(moment().month()).endOf('month').valueOf(); return (time.getTime() > new Date(end)) || (time.getTime() < new Date(begin)); } }

 

 




posted @ 2023-02-01 13:38  森林1997  阅读(133)  评论(0编辑  收藏  举报