uniapp is a cross-platform application development tool based on the Vue.js framework, which can easily develop applications for multiple platforms. In many applications, time selection and calendar display are very common requirements. This article will introduce in detail how to implement time selection and calendar display in the uniapp application, and provide specific code examples.
1. Time selection
<template> <view> <picker mode="time" @change="onSelectTime"></picker> </view> </template> <script> export default { methods: { onSelectTime(e) { console.log('选择的时间为:', e.detail.value) } } } </script>
<template> <view> <picker-view @change="onSelectTime" :value="timeIndex"> <picker-view-column> <view v-for="(hour, index) in hours" :key="index">{{ hour }}</view> </picker-view-column> <picker-view-column> <view v-for="(minute, index) in minutes" :key="index">{{ minute }}</view> </picker-view-column> <picker-view-column> <view v-for="(second, index) in seconds" :key="index">{{ second }}</view> </picker-view-column> </picker-view> </view> </template> <script> export default { data() { return { timeIndex: [0, 0, 0], hours: ['00', '01', '02', ...], minutes: ['00', '01', '02', ...], seconds: ['00', '01', '02', ...] } }, methods: { onSelectTime(e) { const values = e.detail.value const selectedHour = this.hours[values[0]] const selectedMinute = this.minutes[values[1]] const selectedSecond = this.seconds[values[2]] const selectedTime = `${selectedHour}:${selectedMinute}:${selectedSecond}` console.log('选择的时间为:', selectedTime) } } } </script>
2. Calendar display
Calendar display in uniapp is usually implemented using component-based plug-ins. The following is one of the ways.
@vue/calendar
to implement the calendar display function. First, install the plugin:
npm install @vue/calendar --save
Then, introduce the plugin into the page and use:
<template> <view> <vue-calendar></vue-calendar> </view> </template> <script> import VueCalendar from '@vue/calendar' export default { components: { VueCalendar } } </script>
<template> <view> <view class="calendar-header"> <text class="calendar-prev" @click="prevMonth">上个月</text> <text class="calendar-title">{{ currentYear }}年{{ currentMonth }}月</text> <text class="calendar-next" @click="nextMonth">下个月</text> </view> <view class="calendar-weekdays"> <text v-for="(weekday, index) in weekdays" :key="index" class="calendar-weekday">{{ weekday }}</text> </view> <view class="calendar-days"> <text v-for="day in getDaysInMonth(currentYear, currentMonth)" :key="day" class="calendar-day">{{ day }}</text> </view> </view> </template> <script> export default { data() { return { currentYear: new Date().getFullYear(), currentMonth: new Date().getMonth() + 1, weekdays: ['日', '一', '二', '三', '四', '五', '六'] } }, methods: { prevMonth() { // 上个月操作 }, nextMonth() { // 下个月操作 }, getDaysInMonth(year, month) { // 获取某个月份的天数 } } } </script> <style scoped> /* 添加自定义样式 */ </style>
The above are the detailed steps and code examples on how to implement time selection and calendar display in the uniapp application. By using the picker component or a custom time picker, and using a calendar plug-in or a custom calendar component, time selection and calendar display functions can be easily implemented to meet the needs of the application.
The above is the detailed content of How to implement time selection and calendar display in uniapp application. For more information, please follow other related articles on the PHP Chinese website!