最简单的实现方式是使用 HTML5 的原生日期选择器,Vue 可以通过 v-model 进行数…">
当前位置:首页 > VUE

vue实现选择日期

2026-01-23 07:26:20VUE

使用 Vue 实现日期选择功能

使用原生 HTML5 <input type="date">

最简单的实现方式是使用 HTML5 的原生日期选择器,Vue 可以通过 v-model 进行数据绑定:

<template>
  <div>
    <label for="date">选择日期:</label>
    <input type="date" id="date" v-model="selectedDate">
    <p>选择的日期是:{{ selectedDate }}</p>
  </div>
</template>

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

使用第三方日期选择器组件

更推荐使用成熟的第三方日期选择器组件,如 v-calendarelement-ui 的日期选择器。

安装 v-calendar
npm install v-calendar
基本使用示例
<template>
  <div>
    <v-date-picker v-model="selectedDate" />
    <p>选择的日期是:{{ selectedDate }}</p>
  </div>
</template>

<script>
import { DatePicker } from 'v-calendar';

export default {
  components: {
    'v-date-picker': DatePicker
  },
  data() {
    return {
      selectedDate: new Date()
    }
  }
}
</script>
使用 Element UI 的日期选择器

如果项目中使用 Element UI,可以这样实现:

<template>
  <div>
    <el-date-picker
      v-model="selectedDate"
      type="date"
      placeholder="选择日期">
    </el-date-picker>
    <p>选择的日期是:{{ selectedDate }}</p>
  </div>
</template>

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

自定义日期选择器

如果需要完全自定义的日期选择器,可以创建一个组件:

<template>
  <div class="custom-date-picker">
    <div class="calendar-header">
      <button @click="prevMonth">上个月</button>
      <span>{{ currentYear }}年{{ currentMonth }}月</span>
      <button @click="nextMonth">下个月</button>
    </div>
    <div class="calendar-days">
      <div v-for="day in days" :key="day" class="day" @click="selectDate(day)">
        {{ day }}
      </div>
    </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
    },
    days() {
      // 生成当月天数逻辑
      const year = this.currentDate.getFullYear()
      const month = this.currentDate.getMonth()
      const daysInMonth = new Date(year, month + 1, 0).getDate()
      return Array.from({ length: daysInMonth }, (_, i) => i + 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
      )
    },
    selectDate(day) {
      this.selectedDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth(),
        day
      )
      this.$emit('date-selected', this.selectedDate)
    }
  }
}
</script>

<style>
.custom-date-picker {
  width: 300px;
  border: 1px solid #ccc;
  padding: 10px;
}
.calendar-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.calendar-days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.day {
  padding: 5px;
  text-align: center;
  cursor: pointer;
}
.day:hover {
  background-color: #eee;
}
</style>

日期格式化处理

无论使用哪种方式选择日期,通常需要对日期进行格式化处理:

// 在 methods 中
formatDate(date) {
  if (!date) return ''
  const d = new Date(date)
  return `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`
}

或者使用第三方库如 date-fnsmoment.js

import { format } from 'date-fns'

// 使用
format(new Date(), 'yyyy-MM-dd')

这些方法可以根据项目需求选择最适合的实现方式,第三方组件通常提供更丰富的功能和更好的用户体验。

vue实现选择日期

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

相关文章

vue状态管理怎么实现

vue状态管理怎么实现

Vue 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store…

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过…

vue实现字母添加排序

vue实现字母添加排序

实现字母添加排序的方法 在Vue中实现字母添加排序功能,可以通过以下步骤完成: 创建Vue实例并定义数据 new Vue({ el: '#app', data: { letters…

vue懒加载实现难吗

vue懒加载实现难吗

vue懒加载的实现难度 Vue懒加载的实现并不复杂,核心逻辑是通过动态导入(Dynamic Imports)和路由配置或组件异步加载完成。以下是具体实现方法: 路由懒加载实现 在Vue Rout…

vue实现弹窗

vue实现弹窗

Vue 实现弹窗的常见方法 使用组件化方式封装弹窗 创建一个独立的弹窗组件(如 Modal.vue),通过 v-if 或 v-show 控制显示隐藏: <template> <…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…