uni-app 时间周选择器

发布于 1周前 作者 eggper 来自 Uni-App

uni-app 时间周选择器

2020-06-03 18:40

开发环境 版本号 项目创建方式


比如选择2020年第12周,返回第12周的星期一对应的日期星期天对应的日期
4 回复

搞出来了吗


插件开发 Q 1196097915

应用组件 <template>
<view class="p-picker-week">
<c-picker-week></c-picker-week>
</view>
</template>

<script> import CPickerWeek from '@/components/c-picker-week/c-picker-week.vue' export default { components: { CPickerWeek }, data() { return { } }, methods: { } } </script>

组件代码 <template>
<view class="c-picker-week">
<picker mode=“multiSelector” :range=“pickerRange” :value=“pickerValue” @change=“selectYearWeek” @columnchange=“selectYear”>
<view class="c-picker-week__text">{{ showContent ? showContent : ‘时间周选择器’}}</view>
<view class="c-picker-week__text" v-if="selectMonday">选中周周一的日期:{{ selectMonday }}</view>
<view class="c-picker-week__text" v-if="selectSunday">选中周周日的日期:{{ selectSunday }}</view>
</picker>
</view>
</template>

<script> export default { data() { return { pickerRange: [[], []], pickerValue: [0, 0], startWeekDate: '', selectMonday: '', selectSunday: '', showContent: '' }; }, created() { let year = new Date().getFullYear(); // 当前系统年份 let yearList = []; // 根据当前系统年份获取的年份数组,可按需求自行调整,当前规则为系统年份前后10年 for (let i = -10; i < 11; i++) { yearList.push(year + i); } this.pickerRange[0] = yearList; let yearIndex = yearList.findIndex(val => val === year); // 选择器初始选中的年份位置 this.pickerValue[0] = yearIndex; // this.getWeekCountByYear(year.toString()); this.selectYear({ detail: { column: 0, value: yearIndex } }); this.selectYearWeek({ detail: { value: this.pickerValue } }); }, methods: { formatDate(date) { // 格式化日期,将日期转为yyyy-mm-dd的格式 date = new Date(date); let y = date.getFullYear(); let m = this.dateToString(date.getMonth() + 1); let d = this.dateToString(date.getDate()); return y + '-' + m + '-' + d; }, dateToString(date) { // 补0,将日期转为书面格式 date = parseInt(date); if (date < 10) { date = '0' + date; } return date.toString(); }, getWeekCountByYear(yearStart) { /* * 根据传入的年份计算这年有几周,第一周周一的日期 * yearStart (String) 选择的年份 */ let dateStart = new Date(yearStart); // 选择年份的第一天 console.log('!~~~dateStart', dateStart); // console.log('!~~~dateStart-formatDate', this.formatDate(dateStart)); let dateEnd = new Date((parseInt(yearStart) + 1).toString()) - 24*60*60*1000; // 选择年份的最后一天 // console.log('!~~~dateEnd', dateEnd); // console.log('!~~~dateEnd-formatDate', this.formatDate(dateEnd)); let dateStartWeek = dateStart.getDay() === 0 ? 7 : dateStart.getDay(); // 选择年份的第一天是星期几 // console.log('!~~~dateStartWeek', dateStartWeek); /* *根据中华人民共和国国家标准GB/T 7408-2005《数据元和交换格式信息交换日期和时间表示法》中4.3.3.2部分:即一年中的第一个日历星期包括该年的第一个星期四,并且日历年的最后 *一个日历星期就是在下一个日历年的第一个日历星期之前的那个星期,日历星期数是其在该年中的顺序。 */ let startWeekDate = ''; // 第一周的星期一的日期 if (dateStartWeek > 4) { startWeekDate = new Date(dateStart.getTime() + 24*60*60*1000*(7 - dateStartWeek + 1)); } else { startWeekDate = new Date(dateStart.getTime() - 24*60*60*1000*(dateStartWeek - 1)); } // console.log('!~~~startWeekDate-formatDate', this.formatDate(startWeekDate)); this.startWeekDate = this.formatDate(startWeekDate); let lastyearDateStart = new Date((parseInt(yearStart) + 1).toString()); // 选择年份下一年的第一天 // console.log('!~~~lastyearDateStart', lastyearDateStart); let lastyearDateStartWeek = lastyearDateStart.getDay() === 0 ? 7 : lastyearDateStart.getDay(); // 选择年份下一年的第一天是星期几 // console.log('!~~~lastyearDateStartWeek', lastyearDateStartWeek); let lastyearStartWeekDate = ''; // 选择年份下一年的第一周的星期一的日期 if (lastyearDateStartWeek > 4) { lastyearStartWeekDate = new Date(lastyearDateStart.getTime() + 24*60*60*1000*(7 - lastyearDateStartWeek + 1)); } else { lastyearStartWeekDate = new Date(lastyearDateStart.getTime() - 24*60*60*1000*(lastyearDateStartWeek - 1)); } // console.log('!~~~lastyearStartWeekDate-formatDate', this.formatDate(lastyearStartWeekDate)); let weekCount = (lastyearStartWeekDate - startWeekDate)/(7*24*60*60*1000); // 选择的年份有多少周 // console.log('!~~~weekCount', weekCount); console.log(yearStart + '年有' + weekCount + '周,第一周周一的日期是' + this.formatDate(startWeekDate)); let weekList = []; // 选择年份对应的周列表 for (let i = 0; i < weekCount; i++) { weekList.push('第' + (i + 1) + '周'); } // console.log('!~~~weekList', weekList); this.pickerRange[1] = weekList; this.$forceUpdate(); // 重新渲染 }, selectYearWeek(e) { console.log('!~~~e', e); console.log('选择了' + this.pickerRange[0][e.detail.value[0]] + '年' + this.pickerRange[1][e.detail.value[1]]); console.log('!~~~this.startWeekDate', this.startWeekDate); this.selectMonday = this.formatDate(new Date(this.startWeekDate).getTime() + 7*24*60*60*1000*e.detail.value[1]); this.selectSunday = this.formatDate(new Date(this.selectMonday).getTime() + 6*24*60*60*1000); console.log('!~~~selectMonday', this.selectMonday); console.log('!~~~selectSunday', this.selectSunday); this.showContent = '选择了' + this.pickerRange[0][e.detail.value[0]] + '年' + this.pickerRange[1][e.detail.value[1]]; }, selectYear(e) { // console.log('!~~~e', e); // console.log('!~~~e.detail.value', e.detail.value); if (e.detail.column === 0) { this.getWeekCountByYear(this.pickerRange[0][e.detail.value].toString()); } } } } </script> <style lang="scss"> .c-picker-week { .c-picker-week__text { color: #333333; font-size: 32rpx; line-height: 44rpx; text-align: center; } } </style>

在uni-app中实现时间周选择器,可以利用uni-app自带的组件以及JavaScript的日期处理功能来实现。下面是一个简单的代码示例,展示了如何创建一个周选择器,用户可以选择某一年中的某一周。

首先,在页面的<template>部分,我们可以创建一个选择器组件,包含年份和周数的选择:

<template>
  <view class="container">
    <picker mode="selector" :range="years" v-model="selectedYear" @change="onYearChange">
      <view class="picker">{{ selectedYear }}年</view>
    </picker>
    <picker mode="selector" :range="weeksInYear" v-model="selectedWeek" @change="onWeekChange">
      <view class="picker">{{ getWeekDisplay(selectedYear, selectedWeek) }}</view>
    </picker>
  </view>
</template>

接下来,在<script>部分,我们需要定义年份和周数的范围,并处理用户选择的变化:

<script>
export default {
  data() {
    return {
      years: Array.from({ length: 10 }, (_, i) => (new Date()).getFullYear() - 5 + i), // 生成近10年的年份列表
      selectedYear: (new Date()).getFullYear(),
      weeksInYear: this.generateWeeksInYear(this.selectedYear),
      selectedWeek: 1,
    };
  },
  methods: {
    generateWeeksInYear(year) {
      const firstDayOfYear = new Date(year, 0, 1);
      const weekNumberOfYear = Math.ceil((((firstDayOfYear.getDay() + 1) + (365 - firstDayOfYear.getDay() % 7)) / 7));
      return Array.from({ length: weekNumberOfYear }, (_, i) => i + 1);
    },
    getWeekDisplay(year, week) {
      const firstDayOfYear = new Date(year, 0, 1);
      const dayOfWeek = (firstDayOfYear.getDay() + 1) % 7; // 第一天是星期几(0-6)
      const firstDayOfWeek = new Date(year, 0, 1 - dayOfWeek + (week - 1) * 7);
      const lastDayOfWeek = new Date(firstDayOfWeek);
      lastDayOfWeek.setDate(firstDayOfWeek.getDate() + 6);
      return `${firstDayOfWeek.toLocaleDateString()} - ${lastDayOfWeek.toLocaleDateString()}`;
    },
    onYearChange(e) {
      this.selectedWeek = 1; // 重置周数为1
      this.weeksInYear = this.generateWeeksInYear(this.selectedYear);
    },
    onWeekChange(e) {} // 可以在这里处理周数变化后的逻辑
  }
};
</script>

最后,在<style>部分,我们可以添加一些简单的样式:

<style scoped>
.container {
  padding: 20px;
}
.picker {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ccc;
  background-color: #f9f9f9;
}
</style>

这个示例展示了如何使用uni-app的<picker>组件来选择年份和周数,并动态生成某一年中的周数列表。当用户选择年份时,周数列表会自动更新,同时显示所选周数的起始和结束日期。

回到顶部