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

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

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

相关推荐
cn_mengbei几秒前
鸿蒙PC开发实战:Qt环境搭建保姆级教程与常见问题避坑指南(HarmonyOS 4.0+DevEco Studio 3.1最新版)
qt·华为·harmonyos
特立独行的猫a3 分钟前
[鸿蒙PC命令行程序移植]:移植axel多线程高速下载工具踩坑记
华为·harmonyos·移植·鸿蒙pc·axel
Van_Moonlight33 分钟前
RN for OpenHarmony 实战 TodoList 项目:任务完成进度条
javascript·开源·harmonyos
cn_mengbei40 分钟前
从零到一:基于Qt on HarmonyOS的鸿蒙PC原生应用开发实战与性能优化指南
qt·性能优化·harmonyos
Van_Moonlight41 分钟前
RN for OpenHarmony 实战 TodoList 项目:深色浅色主题切换
javascript·开源·harmonyos
俩毛豆42 分钟前
华为的“天工计划”是什么
华为·harmonyos·鸿蒙·搜索·小艺
Van_captain1 小时前
rn_for_openharmony常用组件_Chip纸片
javascript·开源·harmonyos
行者962 小时前
Flutter在鸿蒙平台实现自适应步骤条组件的完整指南
flutter·harmonyos·鸿蒙
小雨青年2 小时前
鸿蒙 HarmonyOS 6 | ArkUI (07):导航架构 Navigation 组件 (V2) 与路由栈管理最佳实践
华为·架构·harmonyos
cn_mengbei2 小时前
鸿蒙PC开发避坑指南:ArkTS原生应用构建全解析与DevEco Studio配置实战
华为·harmonyos