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

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%')
  }
}

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

相关推荐
lqj_本人11 小时前
鸿蒙Cordova开发踩坑记录:跨域请求的“隐形墙“
harmonyos
Z***258015 小时前
HarmonyOS在物联网场景的应用
物联网·华为·harmonyos
Pocker_Spades_A17 小时前
John the Ripper 在 HarmonyOS 上的构建与适配
华为·harmonyos
不爱吃糖的程序媛17 小时前
鸿蒙PC Electron 打印服务实现详解
华为·electron·harmonyos
开源头条20 小时前
2025开源鸿蒙开发者激励计划正式启动,为生态繁荣注入持久动力
华为·开源·harmonyos
GEO_NEWS21 小时前
解析华为Flex:ai的开源棋局
人工智能·华为·开源
奔跑的露西ly1 天前
【HarmonyOS NEXT】自定义样式复用
华为·harmonyos
lqj_本人1 天前
HarmonyOS + Cordova:打包发布与环境差异常见问题指南
华为·harmonyos
不羁的木木1 天前
【开源鸿蒙跨平台开发学习笔记】Day03:React Native 开发 HarmonyOS-GitCode口袋工具开发-1
笔记·学习·harmonyos
lqj_本人1 天前
鸿蒙Cordova开发踩坑记录:震动反馈的“时差“
华为·harmonyos