…">
当前位置:首页 > 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

<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 安装后使用:

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 处理日期格式:

vue实现日期选择

import moment from 'moment'

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

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

注意事项

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

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

相关文章

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基础…

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…