鸿蒙:实现滑动选择日期操作

1、前言

滑动选择日期是我们常用的一种日期选择操作。

2、参考文档

https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-datepickerhttps://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-datepicker

3、核心代码

复制代码
      DatePicker({
        start: new Date('1970-1-1'),
        end: new Date('2100-1-1'),
        selected: this.selectedDate
      })
        .lunar(this.isLunar)
        .onDateChange((value: Date) => {
          this.selectedDate = value;
          console.info('select current date is: ' + value.toString());
        })

4、运行效果

5、完整代码

复制代码
@Entry
@ComponentV2
struct Index {
  @Local isLunar: boolean = false;
  @Local selectedDate: Date = new Date('2021-08-08');

  build() {
    Column() {
      Button('切换公历农历')
        .margin({ top: 30, bottom: 30 })
        .onClick(() => {
          this.isLunar = !this.isLunar;
        })
      Text('当前选择的公历日期是:' + this.selectedDate.getFullYear() + "年" + (this.selectedDate.getMonth() + 1) +
        "月" +
      this.selectedDate.getDate() + "日")


      DatePicker({
        start: new Date('1970-1-1'),
        end: new Date('2100-1-1'),
        selected: this.selectedDate
      })
        .lunar(this.isLunar)
        .onDateChange((value: Date) => {
          this.selectedDate = value;
          console.info('select current date is: ' + value.toString());
        })

    }.width('100%')
  }
}

觉得有帮助可以点赞或收藏

相关推荐
坚果派·白晓明7 小时前
AI驱动的命令行工具集x-cmd鸿蒙化适配后通过DevBox安装使用
人工智能·华为·harmonyos
柒儿吖7 小时前
命令行ninja在鸿蒙PC上的使用方法
华为·harmonyos
hqk12 小时前
鸿蒙ArkUI:状态管理、应用结构、路由全解析
android·前端·harmonyos
ezeroyoung13 小时前
鸿蒙MindSpore Lite 离线模型转换指南
华为·大模型·harmonyos
m0_6855350813 小时前
手机背光模组设计
华为·光学·光学设计·光学工程·镜头设计
大土豆的bug记录14 小时前
鸿蒙实现自定义类似活体检测功能
数码相机·华为·harmonyos·鸿蒙
奔跑的露西ly14 小时前
【HarmonyOS NEXT】顶象验证码 SDK 接入实践
华为·harmonyos
ezeroyoung14 小时前
环信em_chat_uikit(Flutter)适配鸿蒙
flutter·华为·harmonyos
wyw000015 小时前
鸿蒙开发-如何将C++侧接收的PixelMap转换成cv::mat格式
c++·华为·harmonyos
云空16 小时前
《当机器人有了“鸿蒙大脑”:M-Robots OS如何重构产业生态?》
重构·机器人·harmonyos