…">
当前位置:首页 > VUE

vue实现日期选择

2026-02-18 06:15:52VUE

Vue 实现日期选择的方法

使用原生 HTML5 input[type="date"]

Vue 可以直接绑定原生日期输入控件,适合简单场景且不需要复杂样式定制。

<template>
  <input type="date" v-model="selectedDate" @change="handleDateChange">
</template>

<script>
export default {
  data() {
    return {
      selectedDate: ''
    }
  },
  methods: {
    handleDateChange() {
      console.log('Selected date:', this.selectedDate)
    }
  }
}
</script>

使用第三方组件库

主流UI库提供功能丰富的日期选择器:

Element UI

vue实现日期选择

<template>
  <el-date-picker
    v-model="date"
    type="date"
    placeholder="选择日期">
  </el-date-picker>
</template>

<script>
export default {
  data() {
    return {
      date: ''
    }
  }
}
</script>

Vuetify

<template>
  <v-date-picker v-model="picker"></v-date-picker>
</template>

<script>
export default {
  data() {
    return {
      picker: new Date().toISOString().substr(0, 10)
    }
  }
}
</script>

使用专用日期选择库

vue-datepicker 安装后使用:

vue实现日期选择

npm install vue-datepicker --save
<template>
  <date-picker v-model="date"></date-picker>
</template>

<script>
import DatePicker from 'vue-datepicker'

export default {
  components: {
    DatePicker
  },
  data() {
    return {
      date: ''
    }
  }
}
</script>

自定义日期选择组件

如需完全自定义,可基于日历算法实现:

<template>
  <div class="custom-datepicker">
    <div class="calendar-header">
      <button @click="prevMonth">←</button>
      <span>{{ currentYear }}-{{ currentMonth }}</span>
      <button @click="nextMonth">→</button>
    </div>
    <div class="calendar-grid">
      <!-- 日历日期渲染逻辑 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentDate: new Date(),
      selectedDate: null
    }
  },
  computed: {
    currentYear() {
      return this.currentDate.getFullYear()
    },
    currentMonth() {
      return this.currentDate.getMonth() + 1
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() - 1,
        1
      )
    },
    nextMonth() {
      this.currentDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() + 1,
        1
      )
    }
  }
}
</script>

日期格式处理

使用 moment.js 或 date-fns 处理日期格式:

import moment from 'moment'

// 格式化输出
moment(this.selectedDate).format('YYYY-MM-DD')

// 多语言支持
moment.locale('zh-cn')

注意事项

  • 移动端需考虑触摸交互体验
  • 时区处理需保持一致
  • 清空功能需明确交互方式
  • 禁用日期可通过计算属性实现
  • 范围选择需维护开始/结束两个状态值

标签: 日期vue
分享给朋友:

相关文章

vue实现滚动中断

vue实现滚动中断

Vue 实现滚动中断的方法 在 Vue 中实现滚动中断通常涉及监听滚动事件,并在特定条件下阻止默认行为或停止滚动。以下是几种常见方法: 监听滚动事件并阻止默认行为 通过 @scroll 或 @wh…

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vue…

vue实现拖放

vue实现拖放

Vue 实现拖放功能 Vue 中实现拖放功能通常可以通过 HTML5 的原生拖放 API 或第三方库(如 vuedraggable)来实现。以下是两种方法的详细说明。 使用 HTML5 原生拖放…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-fu…