最简单的实现方式是使用 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

vue实现选择日期

import { format } from 'date-fns'

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

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

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

相关文章

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue键盘实现

vue键盘实现

监听键盘事件 在Vue中监听键盘事件可以通过v-on指令或@缩写实现。常见的键盘事件包括keydown、keyup和keypress。例如监听回车键事件: <template> &l…

vue nexttrick实现

vue nexttrick实现

Vue 中 nextTick 的实现原理 nextTick 是 Vue 提供的一个异步方法,用于在下次 DOM 更新循环结束之后执行延迟回调。它利用了 JavaScript 的事件循环机制,确保回调在…