Date-Picker时间插件封装:
<template>
<div class="date-picker">
<div class="date-picker-box">
<div class="date-picker-box-header">
<div class="date-picker-box-header-prev" @click="preMon">{{ '<<' }}</div>
<div class="date-picker-box-header-year">{{ year }}年{{ month }}月</div>
<div class="date-picker-box-header-next" @click="nextMon">{{ '>>' }}</div>
</div>
<div class="date-picker-box-content">
<div class="date-picker-box-content-week">
<div class="date-picker-box-content-weekday">日</div>
<div class="date-picker-box-content-weekday">一</div>
<div class="date-picker-box-content-weekday">二</div>
<div class="date-picker-box-content-weekday">三</div>
<div class="date-picker-box-content-weekday">四</div>
<div class="date-picker-box-content-weekday">五</div>
<div class="date-picker-box-content-weekday">六</div>
</div>
<div class="date-picker-box-content-cont">
<div class="date-picker-box-content-cont-day" v-for="(item, index) in pickerDate" :key="index" :class="{ outfocus: item.outfocus, today: item.showday, start: showStartEnvfun(item.dateNum, item.outfocus), end: showEndEnvfun(item.dateNum, item.outfocus), black: showBlack(item.dateNum, item.outfocus), half: showHalffun(item.dateNum, item.outfocus) }" @click="checkDay(item.dateNum, item.outfocus)">
{{ item.dateNum }}
</div>
</div>
</div>
<div class="date-picker-box-footer">
<div class="date-picker-box-footer-today">今天</div>
<div class="date-picker-box-footer-start">开始</div>
<div class="date-picker-box-footer-end">结束</div>
</div>
<div class="confim">确定</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue';
type Pinks = {
dateNum: number;
outfocus: boolean;
showday?: boolean;
};
let year = ref<number>(0);
let month = ref<number>(0);
let startEnv = ref<string>('');
let endEnv = ref<string>('');
let today = ref<number>(0);
let pickerDate = reactive<Pinks[]>([]);
const getDatePeriod = (sDate1: string, sDate2: string) => {
let oDate1 = new Date(sDate1).getTime();
let oDate2 = new Date(sDate2).getTime();
let iDays = Math.floor(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);
return iDays;
};
const getMonthLen = (year: number, month: number) => {
return new Date(year, month, 0).getDate();
};
const getFirstDay = (year: number, month: number) => {
return new Date(year, month - 1, 1).getDay();
};
const getTwoDay = (date: string) => {
let result = new Date(new Date(date).getTime() + 2 * 24 * 60 * 60 * 1000);
return result.getFullYear() + '-' + (result.getMonth() + 1) + '-' + result.getDate();
};
const getSixDay = (date: string) => {
let result = new Date(new Date(date).getTime() + 6 * 24 * 60 * 60 * 1000);
return result.getFullYear() + '-' + (result.getMonth() + 1) + '-' + result.getDate();
};
const dateCompare = (date1: string, date2: string) => {
var str1 = [];
var str2 = [];
str1 = date1.split('-');
str2 = date2.split('-');
if (parseInt(str1[0]) == parseInt(str2[0]) && parseInt(str1[1]) == parseInt(str2[1]) && parseInt(str1[2]) == parseInt(str2[2])) {
return 3;
} else {
if (parseInt(str1[0]) > parseInt(str2[0])) {
return 1;
} else if (parseInt(str1[0]) < parseInt(str2[0])) {
return 0;
} else {
}
if (parseInt(str1[1]) > parseInt(str2[1])) {
return 1;
} else if (parseInt(str1[1]) < parseInt(str2[1])) {
return 0;
} else {
}
if (parseInt(str1[2]) > parseInt(str2[2])) {
return 1;
} else if (parseInt(str1[2]) < parseInt(str2[2])) {
return 0;
} else {
}
return 0;
}
};
let date = new Date();
year.value = date.getFullYear();
month.value = date.getMonth() + 1;
today.value = date.getDate();
startEnv.value = getTwoDay(year.value + '-' + month.value + '-' + today.value);
endEnv.value = getSixDay(year.value + '-' + month.value + '-' + today.value);
const createCalendar = (year: number, month: number) => {
pickerDate.splice(0, pickerDate.length);
let picks: Array<Pinks> = [];
let currentMonth: boolean = false;
if (date.getFullYear() === year && date.getMonth() + 1 === month) {
currentMonth = true;
} else {
currentMonth = false;
}
let monthStartDate = getFirstDay(year, month);
let lastMonthRestDay = new Date(year, month - 1, 0).getDate();
for (let i = 0; i < monthStartDate; i++) {
picks.push({
dateNum: lastMonthRestDay,
outfocus: true,
});
lastMonthRestDay--;
}
picks = picks.reverse();
let indexMoth = getMonthLen(year, month);
for (let i = 1; i <= indexMoth; i++) {
let showday: boolean = false;
if (currentMonth) {
if (today.value === i) {
showday = true;
} else {
showday = false;
}
}
if (currentMonth && today.value > i) {
picks.push({
dateNum: i,
outfocus: true,
showday: showday,
});
} else {
picks.push({
dateNum: i,
outfocus: false,
showday: showday,
});
}
}
let nextMonLen = 42 - picks.length;
for (var i = 1; i <= nextMonLen; i++) {
picks.push({
dateNum: i,
outfocus: true,
});
}
picks.forEach(item => {
pickerDate.push(item);
});
console.log(picks);
};
createCalendar(year.value, month.value);
const preMon = () => {
if (year.value == date.getFullYear() && month.value <= date.getMonth() + 1) {
return;
}
month.value -= 1;
if (month.value < 1) {
year.value -= 1;
month.value = 12;
}
createCalendar(year.value, month.value);
};
const nextMon = () => {
month.value += 1;
if (month.value > 12) {
year.value = year.value + 1;
month.value = 1;
}
createCalendar(year.value, month.value);
};
const checkDay = (dateNum: number, outfocus: boolean) => {
if (!outfocus) {
let check_day = year.value + '-' + month.value + '-' + dateNum;
if (dateCompare(endEnv.value, check_day) == 0) {
endEnv.value = check_day;
showEndEnvfun(dateNum);
} else if (dateCompare(endEnv.value, check_day) == 3) {
startEnv.value = check_day;
showHalffun(dateNum);
} else if (dateCompare(startEnv.value, check_day) == 3) {
endEnv.value = check_day;
showHalffun(dateNum);
} else if (dateCompare(startEnv.value, check_day) == 1) {
startEnv.value = check_day;
showStartEnvfun(dateNum);
} else if (dateCompare(startEnv.value, check_day) == 0 && dateCompare(endEnv.value, check_day) == 1) {
var disStartEnvLen = getDatePeriod(startEnv.value, check_day) - 1;
var disSEndEnvLen = getDatePeriod(endEnv.value, check_day) - 1;
if (disStartEnvLen > disSEndEnvLen) {
endEnv.value = check_day;
showEndEnvfun(dateNum);
} else {
startEnv.value = check_day;
showStartEnvfun(dateNum);
}
}
}
};
const showStartEnvfun = (dateNum: number, outfocus?: boolean) => {
if (!outfocus) {
if (startEnv.value == year.value + '-' + month.value + '-' + dateNum) {
return true;
} else {
return false;
}
}
};
const showEndEnvfun = (dateNum: number, outfocus?: boolean) => {
if (!outfocus) {
if (endEnv.value == year.value + '-' + month.value + '-' + dateNum) {
return true;
} else {
return false;
}
}
};
const showBlack = (dateNum: number, outfocus?: boolean) => {
if (!outfocus) {
if (dateCompare(startEnv.value, year.value + '-' + month.value + '-' + dateNum) == 0 && dateCompare(year.value + '-' + month.value + '-' + dateNum, endEnv.value) == 0) {
return true;
} else {
return false;
}
}
};
const showHalffun = (dateNum: number, outfocus?: boolean) => {
if (!outfocus) {
if (startEnv.value == year.value + '-' + month.value + '-' + dateNum && endEnv.value == year.value + '-' + month.value + '-' + dateNum) {
return true;
} else {
return false;
}
}
};
</script>
<style lang="less" scoped>
.date-picker {
width: 210px;
height: 230px;
font-size: 14px;
border: 1px solid #ccc;
margin: 100px auto;
.date-picker-box {
.date-picker-box-header {
display: flex;
justify-content: space-between;
padding: 10px;
border-bottom: 1px solid #ccc;
.date-picker-box-header-prev,
.date-picker-box-header-next {
cursor: pointer;
text-align: center;
}
}
.date-picker-box-content {
.date-picker-box-content-week {
display: flex;
align-items: center;
width: 100%;
.date-picker-box-content-weekday {
width: 30px;
text-align: center;
}
}
.date-picker-box-content-cont {
display: flex;
flex-wrap: wrap;
width: 100%;
.date-picker-box-content-cont-day {
width: 30px;
text-align: center;
}
.start {
background: #5ec443;
}
.black {
background: #e2e2e2;
}
.end {
background: #d44040;
}
.outfocus {
color: #e3e3e3;
}
}
}
.date-picker-box-footer {
display: flex;
justify-content: space-between;
padding: 5px 10px;
border-top: 1px solid #ccc;
}
.confim {
text-align: center;
line-height: 20px;
height: 20px;
}
}
}
</style>