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

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

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

相关推荐
梦想不只是梦与想8 小时前
鸿蒙 云测试:上架测试流程
harmonyos·鸿蒙·云测试
传奇开心果编程10 小时前
【ArkUI 练中学】第15课:UI 界面设计与实战
学习·ui·华为·harmonyos
HwJack2011 小时前
【HarmonyOS开发小实践】ArkUI 动画系统属性动画、显式动画与路径动画
ui·性能优化·harmonyos
李游Leo13 小时前
HarmonyOS 7 实战开发 05:把页面做到可上线状态
ios·harmonyos
zhangfeng113316 小时前
ATK(华为算子测试平台)详细介绍 CANN(Compute Architecture for Neural Networks,神经网络计算架构
人工智能·华为·ai编程·npu·cann
贾伟康16 小时前
【HarmonyOS 7新能力|043】可变字体工程封装:把接入逻辑放进可维护的分层结构
harmonyos·arkts·arkui·ui设计·可变字体
HarmonyOS_SDK16 小时前
HarmonyOS智慧多窗,让应用在任意窗口都“恰到好处”
harmonyos
zhangfeng113317 小时前
《从“人工适配“到“智能生成“:KernelSwift 跨国产芯片算子迁移全栈方案解读》 —— 强调范式跃迁和跨硬件属性,适合偏架构分析的写法
人工智能·算法·华为·ai编程·npu
传奇开心果编程19 小时前
【ArkUI 练中学】第14课:应用性能优化实战
学习·ui·华为·harmonyos
用户5930960097819 小时前
Flutter 鸿蒙化实战:flutter_tts 适配 OpenHarmony,文本转语音
harmonyos