作者:曹琪娟
本文正在参加星光计划3.0–夏日挑战赛
前言
目前工作中同一个项目经常需要同时开发FA和H5两种形态。H5使用的主要是vue框架,FA则使用 HarmonyOS ArkUI(JS) 。HarmonyOS ArkUI(JS) 开发框架设计思想和语法与vue很类似,经常看到很多vue相关的日历控件,想着基于现掌握的 FA 相关知识,也尝试着编写一个简单的日历选择控件。由于最近忙项目,时间匆忙,就只是实现了一些简单的功能。
效果展示

实现代码
1. htm 代码
主要分为2个部分,日期输入框和日期选择面板。点击日期输入框会弹出日期选择面板。日期选择面板的日期部分固定为7 * 6 = 42 个日期进行填充。
2. js 代码
js 主要计算出当前月的总天数和上个月要填充的天数和下个月要填充的天数,把这些日期值存放在一个日期列表里,用于UI的循环渲染显示。
export default {
data: {
isShowPickDate: false,
monthText: '',
tempValue: null,
inputValue: null,
weekdays: ['一', '二', '三', '四', '五', '六','日',]
},
onInit() {
},
computed: {
getDays () {
const [year, month] = this.getYearMonthDay(this.tempValue);
let startWeek = new Date(year, month, 1).getDay();
if (startWeek === 0) {
startWeek = 7;
}
const prevLastDay = this.getPrevMonthLastDay(year, month);
const curLastDay = this.getCurrentMonthLastDay(year, month);
this.monthText = `${year}年${month + 1}月`
const days = [...this.getPrevMonthDays(prevLastDay, startWeek), ...this.getCurrentMonthDays(curLastDay), ...this.getNextMonthDays(curLastDay, startWeek)];
return days;
},
formatDate () {
const [year, month, day] = this.getYearMonthDay(this.inputValue);
return { year, month: month + 1, day };
},
inputData() {
return `${this.formatDate.year}-${this.formatDate.month}-${this.formatDate.day}`
}
},
showPickDate() {
this.isShowPickDate = true;
console.log('显示日期选择面板');
this.tempValue = this.inputValue;
},
dayClick(e) {
console.log('当前选择的日期');
console.log(JSON.stringify(e))
if (e.status !== 'current') return;
this.tempValue = e.date;
this.inputValue = this.tempValue;
this.isShowPickDate = false;
},
getDate(item) {
return item.date.getDate();
},
changeMonth(value) {
console.log('value' + value)
const [year, month] = this.getYearMonthDay(this.tempValue);
const monthNum = month + value;
const val = this.tempValue.setMonth(monthNum)
this.tempValue = new Date(val)
console.log(this.tempValue)
},
getPrevMonthDays (prevLastDay, startWeek) {
const [year, month] = this.getYearMonthDay(this.tempValue);
const prevMonthDays = [];
console.log('prevLastDay' + prevLastDay)
for (let i = prevLastDay - startWeek+2; i <= prevLastDay; i++) {
prevMonthDays.push({
date: new Date(year, month - 1, i),
status: 'prev'
});
}
console.log('prevMonthDays' + prevMonthDays.length);
return prevMonthDays;
},
getCurrentMonthDays (curLastDay) {
const [year, month] = this.getYearMonthDay(this.tempValue);
const curMonthDays = [];
for (let i = 1; i <= curLastDay; i++) {
curMonthDays.push({
date: new Date(year, month, i),
status: 'current'
});
}
console.log('curMonthDays' + curMonthDays.length);
return curMonthDays;
},
getNextMonthDays (curLastDay, startWeek) {
const [year, month] = this.getYearMonthDay(this.tempValue);
const nextMonthDays = [];
for (let i = 1; i <= 42 - startWeek - curLastDay + 1; i++) {
nextMonthDays.push({
date: new Date(year, month + 1, i),
status: 'next'
});
}
return nextMonthDays;
},
getCurrentMonthLastDay(year, month){
return new Date(year, month + 1, 0).getDate();
},
getPrevMonthLastDay(year, month){
return new Date(year, month, 0).getDate();
},
getYearMonthDay(value) {
const date = value ? new Date(value) : new Date();
const day = date.getDate();
const month = date.getMonth();
const year = date.getFullYear();
return [year, month, day];
},
isToday (date) {
const [y1, m1, d1] = this.getYearMonthDay(date);
const [y2, m2, d2] = this.getYearMonthDay(new Date);
return y1 === y2 && m1 === m2 && d1 === d2;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
3. css 样式
总结
到这里简单的日期选择控件基本完成,这里的主要技术点就是对日期相关api的掌握,日期相关的计算和要清楚实现思路,其中还有很多不足,还有很多可优化的地方,后续有时间还会慢慢去研究,完善。也欢迎各位开发者一起讨论与研究,希望本次分享对大家有所帮助,如有问题欢迎指正。
入门到精通、技巧到案例,系统化分享HarmonyOS开发技术,欢迎投稿和订阅,让我们一起携手前行共建鸿蒙生态。
刚好需要一个日历组件来做项目,收藏收藏