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

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

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

相关推荐
hashiqimiya13 分钟前
harmonyos的鸿蒙的跳转页面的部署参数传递
华为·harmonyos
一点七加一3 小时前
Harmony鸿蒙开发0基础入门到精通Day13--ArkScript篇
华为·harmonyos
程序员老刘3 小时前
Flutter官方拒绝适配鸿蒙的真相:不是技术问题,而是...
flutter·harmonyos·客户端
平平不平凡4 小时前
鸿蒙组件分级指南:从细胞到思维的系统化角色解析
harmonyos
m0_685535086 小时前
手机镜头设计
华为·光学·光学设计·光学工程·镜头设计
爱笑的眼睛117 小时前
HarmonyOS Marquee组件深度解析:构建高性能滚动视觉效果
华为·harmonyos
不爱说话郭德纲7 小时前
UniappX不会运行到鸿蒙?超超超保姆级鸿蒙开发生成证书以及配置证书步骤
前端·uni-app·harmonyos
m0_685535087 小时前
大疆无人机镜头
华为·光学·光学设计·光学工程·镜头设计
m0_685535087 小时前
无人机镜头设计实例
华为·光学·光学设计·光学工程·镜头设计
爱笑的眼睛118 小时前
HarmonyOS中的智能路线规划与导航应用开发:利用分布式架构构建跨设备体验
华为·harmonyos