首页 > 其他分享 >年月日时间范围选择框

年月日时间范围选择框

时间:2023-08-04 11:57:07浏览次数:28  
标签:console log resultDate month 选择 let date 年月日 范围

 

        <view class="search-bar-box" style="background: #fff;" @click="openPicker">
            <view style="width:100%;height:100%;padding:0 24rpx;">
                <sofar-picker :visable.sync="pickerVisable" :defaultDate="defaultDate" :minYear="1990" separator="."
                    themeColor="#10BE9D" startText="开始时间" endText="结束时间" @confirm="confirm"
                    @defaultDate1="defaultDate1">
                </sofar-picker>
            </view>
        </view>
            // 打开日期选择框
            openPicker() {
                this.pickerVisable = true
            },

 

<template>
    <view :class="{'pickerMask':visable}" @click="maskClick" @touchmove.stop.prevent="returnHandle">
        <input type="text" v-model="defaultDate1" disabled>
        <view class="picker-box" :class="{'picker-show':visable}">
            <view class="operate-box" @touchmove.stop.prevent="returnHandle" @tap.stop="returnHandle">
                <view class="time-box">
                    <view @click="touchSelect(0)" class="time-item" :style="{color:touchIndex?'#000000':themeColor}">
                        <text>{{startText}}</text>
                        <text>{{resultDate[0]}}</text>
                    </view>
                    <text>至</text>
                    <view @click="touchSelect(1)" class="time-item" :style="{color:touchIndex?themeColor:'#000000'}">
                        <text>{{endText}}</text>
                        <text>{{resultDate[1]}}</text>
                    </view>
                </view>
                <view :style="{color:themeColor}" @click="pickerConfirm">确定</view>
            </view>
            <picker-view :value="pickerValue" @change="pickerChange" class="picker-view"
                :indicator-style="indicatorStyle" @tap.stop="returnHandle">
                <picker-view-column>
                    <view class="picker-item" v-for="(item,index) in yearList" :key="index">
                        {{item+'年'}}</view>
                </picker-view-column>
                <picker-view-column>
                    <view class="picker-item" v-for="(item,index) in monthList" :key="index">
                        <text>{{item}}月</text>
                    </view>
                </picker-view-column>
                <picker-view-column>
                    <view class="picker-item" v-for="(item,index) in dayList" :key="index">
                        <text>{{item}}日</text>
                    </view>
                </picker-view-column>
            </picker-view>
        </view>
    </view>
</template>

<script>
    export default {
        name: 'sofarPicker',
        props: {
            defaultDate: {
                type: Array,
                default: () => []
            },
            visable: {
                type: Boolean,
                default: false
            },
            minYear: {
                type: Number,
                default: 1990,
            },
            separator: {
                type: String,
                default: '.'
            },
            themeColor: {
                type: String,
                default: '#10BE9D'
            },
            startText: {
                type: String,
                default: '开始时间'
            },
            endText: {
                type: String,
                default: '结束时间'
            }
        },
        data() {
            const date = new Date();
            const yearList = [];
            const year = date.getFullYear();
            const monthList = [];
            const month = date.getMonth() + 1;
            const day = date.getDate()
            for (let i = this.minYear; i <= date.getFullYear(); i++) {
                yearList.unshift(i);
            }
            for (let i = 1; i <= 12; i++) {
                monthList.push(i);
            }
            
            return {
                indicatorStyle: 'height: 100rpx;',
                touchIndex: 0,
                yearList: yearList,
                monthList: monthList,
                dayList:[],
                year: year,
                month: month,
                day:day,
                pickerValue: [0, month - 2,0],
                resultDate: [],
                defaultDate1:[],
            }
        },
        mounted() {
            this.setDate()
        },
        created() {
            // this.defaultDate1 = this.defaultDate
            console.log(this.pickerValue,97)
        },
        methods: {
            returnHandle() {},
            maskClick() {
                this.$emit('update:visable', false)
            },
            setDate() {
                console.log(this.defaultDate,105)
                if (this.defaultDate.length > 0) {
                    let date = this.defaultDate[0]
                    this.resultDate = this.defaultDate
                    console.log(this.resultDate,116)
                    this.setPicker(date)
                } else {
                    let startTime = this.year + this.separator + (this.month-1) + this.separator + 1
                    let endTime = this.year + this.separator + this.month + this.separator + 1
                    this.resultDate = [startTime, endTime]
                    console.log(this.resultDate,122)
                    this.defaultDate1 = startTime + ' - ' + endTime
                    console.log(this.defaultDate1,115)
                    this.$emit('defaultDate1',[startTime,endTime])
                    this.setDayData(this.pickerValue[1] + 1)
                }
            },
            setDayData(day){
                let num = 0;
                let months = [1, 3, 5, 7, 8, 10, 12]
                let month = day
                if(months.indexOf(month) > -1){
                    num = 31;
                }else{
                     num = 30
                }
                if (month === 2) {
                  if (this.yearList[0] % 400 === 0 || this.yearList[0] % 4 === 0 && this.yearList[0] % 100 !== 0) {
                   num = 29;
                  } else {
                   num = 28;
                  }
                }
                let arr = []
                for (let i = 1; i < num + 1; i++) {
                    arr.push(i);
                }
                this.dayList = arr
                // console.log(arr,171)
            },
            setPicker(date) {
                console.log(date,119)
                let dateArray = date.split(this.separator)
                let yearIndex = this.yearList.indexOf(Number(dateArray[0]))
                let monthIndex = this.monthList.indexOf(Number(dateArray[1]))
                let dayIndex = this.dayList.indexOf(Number(dateArray[2]))
                this.pickerValue = [yearIndex, monthIndex,dayIndex]
                console.log(this.pickerValue,160)
            },
            getDate(date) {
                console.log(date,161)
                let result = ''
                let year = this.yearList[date[0]]
                let month = this.monthList[date[1]]
                let day =  this.dayList[date[2]]
                
                result = year + this.separator + month + this.separator + day
                this.resultDate[this.touchIndex] = result
                console.log(result,133)
                this.setDayData(this.pickerValue[1] + 1)
            },
            touchSelect(val) {
                let date = this.resultDate[val]
                if (this.touchIndex === val) {
                    return
                } else {
                    this.touchIndex = val
                }
                if (date) {
                    this.setPicker(date)
                }
            },
            pickerChange(e) {
                this.pickerValue = e.detail.value
                console.log(this.pickerValue,189)
                this.getDate(e.detail.value)
            },
            pickerConfirm() {
                const {
                    resultDate,
                    year,
                    month,
                    day
                } = this
                let nowTime = new Date(year + '.' + month + '.' + day).getTime()
                let startTime = new Date(resultDate[0]).getTime()
                let endTime = new Date(resultDate[1]).getTime()
                console.log(resultDate,200)
                console.log(resultDate[0].split('.'),resultDate[1].split('.'))
                if (startTime <= endTime && endTime <= nowTime) {
                    if(resultDate[0].split('.')[0] !== resultDate[1].split('.')[0]){
                        uni.showToast({
                            title: '不能跨年选择!',
                            icon: 'none'
                        })
                    }else{
                        this.$emit('confirm', resultDate)
                        this.defaultDate1 = resultDate[0] + ' - ' + resultDate[1]
                        console.log(this.defaultDate1,199)
                        this.maskClick()
                    }
                } else {
                    uni.showToast({
                        title: '时间范围错误!',
                        icon: 'none'
                    })
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .pickerMask {
        position: fixed;
        z-index: 998;
        top: 0;
        right: 0;
        left: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.6);
    }

    .picker-box {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        transition: all 0.3s ease;
        transform: translateY(100%);
        z-index: 998;

        .operate-box {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 18rpx 30rpx;
            background-color: #FFFFFF;
            text-align: center;
            font-size: 30rpx;
            border-bottom: 2rpx solid #e5e5e5;

            .time-box {
                width: 60%;
                display: flex;
                align-items: center;
                justify-content: space-between;

                .time-item {
                    display: flex;
                    flex-direction: column;
                }
            }
        }
    }

    .picker-show {
        transform: translateY(0);
    }

    .picker-view {
        width: 750rpx;
        height: 600rpx;
        background-color: #FFFFFF;

        .picker-item {
            height: 100rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
        }
    }

    /deep/ .uni-input-input {
        height: 64rpx;
        line-height: 64rpx;
        position: absolute;
        top: 0;
    }
</style>

 

标签:console,log,resultDate,month,选择,let,date,年月日,范围
From: https://www.cnblogs.com/gwkzb/p/17605499.html

相关文章

  • 选择器
    CSS(CascadingStyleSheets,层叠样式表)中包含各种类型的选择器,用于选择要应用样式的HTML元素。选择器定义了哪些元素将受到CSS规则的影响,从而可以控制元素的外观和样式。以下是一些常见的CSS选择器类型:元素选择器:选择特定标签的元素。例如,div会选择所有<div>元素。ID选择......
  • 椭圆PF1✖️PF2的范围
    题目设P\((x_0,y_0)\)是椭圆C:$x^2\overb^2$\(+{y^2\overb^2}\)\(=1\)\((a>b>0)\)上一点,且\(\angleF_1PF_2\)\(=\theta.\)求\(PF_1\)*\(PF_2\)取值范围。失败的思路读题读一半的屑准备用基本不等式,发现只能算个最大值\(a^2\)做法一焦半径公式\(PF_1\)*\(PF_2\)\(=......
  • LightGBM为什么比xgbost好?——选择梯度大(残差大)样本来进行特征分裂生成的树,借鉴了Ad
    LightGBM(LightGradientBoostingMachine)是一款基于决策树算法的分布式梯度提升框架。为了满足工业界缩短模型计算时间的需求,LightGBM的设计思路主要是两点:减小数据对内存的使用,保证单个机器在不牺牲速度的情况下,尽可能地用上更多的数据;减小通信的代价,提升多机并行时的效率,实现在......
  • 动态规划--选择问题
    1.路径选择1.1.HouseRobber给一个自然数数组,在不允许相邻取的情况下,求可取的最大和Input:[1,2,3,1]Output:4取1,3和为4方法:设定状态dp[n]表示前n项在不能相邻取情况下最大和取法的最大和(结果),要用前面信息表示dp[n]\[dp[n]=max(value[n]+dp[n-2],dp[n-1])\]defr......
  • 性能测试怎么做?测试工具怎么选择?
    在当前软件测试行业,熟练掌握性能测试已经是测试工程师们面试的敲门砖了,当然还有很多测试朋友们每天的工作更多的是点点点,性能方面可能也只是做过简单的并发测试,对于编写脚本,搭建环境方面也比较陌生。今天这篇文章就给大家梳理一下如何去做性能测试,和怎么熟练掌握性能测试。文章结构......
  • 性能测试怎么做?测试工具怎么选择?
    在当前软件测试行业,熟练掌握性能测试已经是测试工程师们面试的敲门砖了,当然还有很多测试朋友们每天的工作更多的是点点点,性能方面可能也只是做过简单的并发测试,对于编写脚本,搭建环境方面也比较陌生。今天这篇文章就给大家梳理一下如何去做性能测试,和怎么熟练掌握性能测试。文章结......
  • 在DIALOG菜单栏里设置的全选(取消全选)或选择功能
     全选和取消全选: 选择和取消选择: ......
  • 少有人走的道路之选择大于努力
    在人生的旅程中,我们经常被告诫要努力奋斗,追求成功和成就。然而,成功往往不仅仅由努力决定,而更多地取决于我们所做的选择。选择决定了我们的方向和目标,决定了我们是否能够走上少有人走的道路,独创出一番精彩。本文将探讨在选择与努力之间的关系中,为什么选择大于努力,并阐述如何通过明......
  • 定义命运的决策:你的选择塑造你的未来
    命运是每个人都经历的不可预知的旅程。然而,我们的选择却是塑造这一旅程的重要因素。每个人都拥有决策的自由,无论是小至日常的选择还是大至重大的人生决策,都会对我们的未来产生深远的影响。命运,作为人们所认知的未来的走向和结果,往往被看作是不可抗拒的。然而,我们往往忽视了决策在......
  • jQuery--dom对象选择器
    一、概述选择器:就是一个字符串,用来定位dom对象定位了dom对象就可以通过jquery的函数操作dom对象二、常用选择器1、id选择器$("#id值")通过dom对象的id定位dom对象。id是当前页面唯一值 2、class选择器$(".class样式名")使用样式的名称定位dom对象3、标签选择器$("标签名......