momentjs是一个处理时间的js库,简洁易用。
浅析一下, momentjs 在vue中对DatePicker时间组件的禁用实践。
一,npm下载
npm install moment --save
二,particles.json中
"dependencies": {"axios": "^0.18.0","iview": "^3.4.0","moment": "^2.24.0","vue": "^2.5.10","vue-i18n": "^7.8.0","vue-router": "^3.0.1","vuex": "^3.0.1"},
三,引入使用
1.main.js中引入
import 'moment/locale/zh-cn'
moment.locale('zh-cn');
Vue.prototype.$moment = moment;
2.页面或组件引入使用
import moment from "moment"
3.方法使用
moment().format('YYYY-MM-DD'); //获取格式
moment().startOf('month') //获取当月第一天
moment().endOf('month') //获取当月最后一天
moment().subtract(7, 'days') //获取前7天
moment().subtract(4,'month') //获取前4个月
更多方法参考momentjs官网:momentjs
4.应用实例
a,日期禁用
pickerStart: {disabledDate(time) {let startDate;let endDate=moment().subtract(4, "days").format("YYYY-MM-DD");if(today<4){startDate=moment().subtract(5,'month').endOf('month').format('YYYY-MM-DD');}if(today>4||today==4){startDate=moment().subtract(4,'month').endOf('month').format('YYYY-MM-DD');}return (time && time.valueOf() < new Date(startDate).getTime() ||time.valueOf() > new Date(endDate).getTime());}},pickerEnd: {disabledDate(time) {let startDate;let endDate=moment().subtract(4, "days").format("YYYY-MM-DD");if(today<4){startDate=moment().subtract(5,'month').endOf('month').format('YYYY-MM-DD');}if(today>4||today==4){startDate=moment().subtract(4,'month').endOf('month').format('YYYY-MM-DD');}return (time && time.valueOf() < new Date(startDate).getTime() ||time.valueOf() > new Date(endDate).getTime());},},
b,月份禁用
pickerStart: {disabledDate(time) {let startMonth;let endMonth;if(today<4){startMonth=moment().subtract(5, "month").format("YYYY-MM");endMonth=moment().subtract(2, "month").format("YYYY-MM");}if(today>4||today==4){startMonth=moment().subtract(4, "month").format("YYYY-MM");endMonth=moment().subtract(1, "month").format("YYYY-MM");}return (time && time.valueOf() < new Date(startMonth).getTime() ||time.valueOf() > new Date(endMonth).getTime());}},pickerEnd: {disabledDate(time) {let startMonth;let endMonth;if(today<4){startMonth=moment().subtract(5, "month").format("YYYY-MM");endMonth=moment().subtract(2, "month").format("YYYY-MM");}if(today>4||today==4){startMonth=moment().subtract(4, "month").format("YYYY-MM");endMonth=moment().subtract(1, "month").format("YYYY-MM");}return (time && time.valueOf() < new Date(startMonth).getTime() ||time.valueOf() > new Date(endMonth).getTime());},},
四,页面效果
a,禁用月份
b,禁用日期
五,完整代码
1,页面查询组件引用
a.引入
import DateSearch from "_c/date-search";
components: {DateSearch},
b.渲染
<date-search ref="dateSearch"><span class="search-label">查询时间</span></date-search>
c.获取时间
let {S_createTime_GTOE, S_createTime_LTOE} = this.$refs.dateSearch.getDateSearch();this.queryParam.endTime =S_createTime_LTOE;this.queryParam.startTime=S_createTime_GTOE;
2.时间组件
a.月份组件
<template><span><slot /><FormItem><DatePickerv-model="condition.S_createTime_GTOE"confirmclearableplacement="bottom-end"class="search-input":type="type":format="format":options="pickerStart"transfer@on-change="onStartDateChange"></DatePicker></FormItem><span class="search-label">至</span><FormItem><DatePickerv-model="condition.S_createTime_LTOE"confirmclearableplacement="bottom-end":type="type":format="format"class="search-input":options="pickerEnd"transfer@on-change="onEndDateChange"></DatePicker></FormItem></span>
</template>
<script>
import { convertUTCTimeToMonth } from "@/libs/tools";
import moment from "moment"
export default {props: {format: {type: String,default: "yyyy-MM",},type: {type: String,default: "month",},defaultTime: {type: Boolean,default: true,},searchField: {type: String,default: "createTime",},},data() {const today= moment().format('DD');return {condition: {S_createTime_GTOE: "",S_createTime_LTOE: "",},pickerStart: {disabledDate(time) {let startMonth;let endMonth;if(today<4){startMonth=moment().subtract(5, "month").format("YYYY-MM");endMonth=moment().subtract(2, "month").format("YYYY-MM");}if(today>4||today==4){startMonth=moment().subtract(4, "month").format("YYYY-MM");endMonth=moment().subtract(1, "month").format("YYYY-MM");}return (time && time.valueOf() < new Date(startMonth).getTime() ||time.valueOf() > new Date(endMonth).getTime());}},pickerEnd: {disabledDate(time) {let startMonth;let endMonth;if(today<4){startMonth=moment().subtract(5, "month").format("YYYY-MM");endMonth=moment().subtract(2, "month").format("YYYY-MM");}if(today>4||today==4){startMonth=moment().subtract(4, "month").format("YYYY-MM");endMonth=moment().subtract(1, "month").format("YYYY-MM");}return (time && time.valueOf() < new Date(startMonth).getTime() ||time.valueOf() > new Date(endMonth).getTime());},},};},methods: {getDateSearch() {const defaultTime = this.defaultTime;let valid = true;let { S_createTime_GTOE, S_createTime_LTOE } = this.condition;if (S_createTime_GTOE && typeof S_createTime_GTOE === "object") {S_createTime_GTOE = convertUTCTimeToMonth(S_createTime_GTOE, true);}if (S_createTime_LTOE && typeof S_createTime_LTOE === "object") {S_createTime_LTOE = convertUTCTimeToMonth(S_createTime_LTOE, true);}if (defaultTime) {if (!S_createTime_GTOE) {this.$Message.warning("查询起始时间不能为空");valid = false;} else if (!S_createTime_LTOE) {this.$Message.warning("查询结束时间不能为空");valid = false;} else if (S_createTime_LTOE < S_createTime_GTOE) {this.$Message.warning("结束时间不能早于起始时间");valid = false;} } else {if (S_createTime_LTOE && S_createTime_LTOE < S_createTime_GTOE) {this.$Message.warning("结束时间不能早于起始时间");valid = false;}}return {valid,[`S_${this.searchField}_GTOE`]: S_createTime_GTOE,[`S_${this.searchField}_LTOE`]: S_createTime_LTOE,};},onStartDateChange(date) {this.condition.S_createTime_GTOE = date;},onEndDateChange(date) {this.condition.S_createTime_LTOE = date;},getDefaultTime() {let startMonth;let endMonth;let today= moment().format('DD'); //30if(today<4){startMonth=moment().subtract(4, "month").format("YYYY-MM");endMonth=moment().subtract(2, "month").format("YYYY-MM");}if(today>4||today==4){startMonth=moment().subtract(3, "month").format("YYYY-MM");endMonth=moment().subtract(1, "month").format("YYYY-MM");}return [startMonth,endMonth]},resetDateSearch(){if(this.defaultTime){this.condition.S_createTime_GTOE = this.getDefaultTime()[0],this.condition.S_createTime_LTOE = this.getDefaultTime()[1]}else{this.condition.S_createTime_GTOE = ""this.condition.S_createTime_LTOE = ""}}},created() {this.resetDateSearch();},
};
</script>
convertUTCTimeToMonth 方法
// 时间到月(年/月)
export const convertUTCTimeToMonth = (UTCDateString) => {if (!UTCDateString) {return '-'}function formatFunc (str) { return str > 9 ? str : '0' + str}let date2 = new Date(UTCDateString)let year = date2.getFullYear()let mon = formatFunc(date2.getMonth() + 1)let day = formatFunc(date2.getDate())let hour = date2.getHours()hour = formatFunc(hour)let min = formatFunc(date2.getMinutes())let second = formatFunc(date2.getSeconds())let dateStr = year + '-' + monreturn dateStr
}
b.日期组件
<template><span><slot /><FormItem><DatePickerv-model="condition.S_createTime_GTOE"confirmclearableplacement="bottom-end"class="search-input":type="type":format="format":options="pickerStart"transfer@on-change="onStartDateChange"></DatePicker></FormItem><span class="search-label">至</span><FormItem><DatePickerv-model="condition.S_createTime_LTOE"confirmclearableplacement="bottom-end":type="type":format="format"class="search-input":options="pickerEnd"transfer@on-change="onEndDateChange"></DatePicker></FormItem></span>
</template>
<script>
import { convertUTCTimeToDate } from "@/libs/tools";
import moment from "moment"
export default {props: {format: {type: String,default: "yyyy-MM-dd",},type: {type: String,default: "date",},defaultTime: {type: Boolean,default: true,},searchField: {type: String,default: "createTime",},},data() {const today= moment().format('DD');return {condition: {S_createTime_GTOE: "",S_createTime_LTOE: "",},pickerStart: {disabledDate(time) {let startDate;let endDate=moment().subtract(4, "days").format("YYYY-MM-DD");if(today<4){startDate=moment().subtract(5,'month').endOf('month').format('YYYY-MM-DD');}if(today>4||today==4){startDate=moment().subtract(4,'month').endOf('month').format('YYYY-MM-DD');}return (time && time.valueOf() < new Date(startDate).getTime() ||time.valueOf() > new Date(endDate).getTime());}},pickerEnd: {disabledDate(time) {let startDate;let endDate=moment().subtract(4, "days").format("YYYY-MM-DD");if(today<4){startDate=moment().subtract(5,'month').endOf('month').format('YYYY-MM-DD');}if(today>4||today==4){startDate=moment().subtract(4,'month').endOf('month').format('YYYY-MM-DD');}return (time && time.valueOf() < new Date(startDate).getTime() ||time.valueOf() > new Date(endDate).getTime());},},};},methods: {getDateSearch() {const defaultTime = this.defaultTime;let valid = true;let { S_createTime_GTOE, S_createTime_LTOE } = this.condition;if (S_createTime_GTOE && typeof S_createTime_GTOE === "object") {S_createTime_GTOE = convertUTCTimeToDate(S_createTime_GTOE, true);}if (S_createTime_LTOE && typeof S_createTime_LTOE === "object") {S_createTime_LTOE = convertUTCTimeToDate(S_createTime_LTOE, true);}if (defaultTime) {if (!S_createTime_GTOE) {this.$Message.warning("查询起始时间不能为空");valid = false;} else if (!S_createTime_LTOE) {this.$Message.warning("查询结束时间不能为空");valid = false;} else if (S_createTime_LTOE < S_createTime_GTOE) {this.$Message.warning("结束时间不能早于起始时间");valid = false;} } else {if (S_createTime_LTOE && S_createTime_LTOE < S_createTime_GTOE) {this.$Message.warning("结束时间不能早于起始时间");valid = false;}}return {valid,[`S_${this.searchField}_GTOE`]: S_createTime_GTOE.split(" ")[0],[`S_${this.searchField}_LTOE`]: S_createTime_LTOE.split(" ")[0],};},onStartDateChange(date) {this.condition.S_createTime_GTOE = date;},onEndDateChange(date) {this.condition.S_createTime_LTOE = date;},getDefaultTime() {let startDate;let today= moment().format('DD'); //30let endDate=moment().subtract(4, "days").format("YYYY-MM-DD");if(today<4){startDate=moment().subtract(4,'month').startOf('month').format('YYYY-MM-DD');}if(today>4||today==4){startDate=moment().subtract(3,'month').startOf('month').format('YYYY-MM-DD');}return [startDate,endDate]},resetDateSearch(){if(this.defaultTime){this.condition.S_createTime_GTOE = this.getDefaultTime()[0],this.condition.S_createTime_LTOE = this.getDefaultTime()[1]}else{this.condition.S_createTime_GTOE = ""this.condition.S_createTime_LTOE = ""}}},created() {this.resetDateSearch();},
};
</script>
convertUTCTimeToDate
// 时间到日期(年/月/日)
export const convertUTCTimeToDate = (UTCDateString) => {if (!UTCDateString) {return '-'}function formatFunc (str) {return str > 9 ? str : '0' + str}let date2 = new Date(UTCDateString)let year = date2.getFullYear()let mon = formatFunc(date2.getMonth() + 1)let day = formatFunc(date2.getDate())let hour = date2.getHours()hour = formatFunc(hour)let min = formatFunc(date2.getMinutes())let second = formatFunc(date2.getSeconds())let dateStr = year + '-' + mon + '-' + dayreturn dateStr
}
<完>