uniapp 日期选择器
时间: 2023-09-09 10:06:14 浏览: 121
日期选择器
在Uniapp中,你可以使用picker组件来实现日期选择器功能。以下是一个示例代码:
```
<picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate" @change="bindDateChange">
<view>{{ date }}</view>
</picker>
<script>
function getDate(type) {
const date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
if (type === 'start') {
year = year - 10;
} else if (type === 'end') {
year = year + 10;
}
month = month > 9 ? month : '0' + month;
return `${year}-${month}`;
}
export default {
data() {
return {
date: getDate({ format: true }),
startDate: getDate('start'),
endDate: getDate('end'),
};
},
methods: {
bindDateChange(e) {
this.date = e.detail.value.substr(0, 7);
this.time = this.date;
this.page = 1;
this.list = [];
this.index();
},
},
};
</script>
```
在上述代码中,你可以看到picker组件的用法。通过设置fields属性为"month",你只能选择年月。如果想要选择年,则可以将fields属性设置为"year"。默认情况下,picker组件可以选择年月日格式。如果你希望同时显示每天,你可以将标签中的fields="month"删除,并在getDate函数中修改返回值为`${year}-${month}-${day}`。这样就可以实现日期选择器的功能了。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [uniapp日期选择器](https://blog.csdn.net/m0_61672533/article/details/127693169)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文