HarmonyOS 如何获取格式化的时间?

获取这3个时间

1.yyyyMMddHHmmssSSS

20240730150810123

2.yyyyMMdd

20240730

3.HHmmss

150910

以上三个格式化的时间怎么获取

看这个文档,

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/i18n-time-date-V5

实现不了,麻烦提供一下以上三个的日期和时间的获取方法

HarmonyOS
2024-12-25 18:25:09
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

推荐使用第三方库dayjs,完整代码如下:

import dayjs from "dayjs";

@Entry
@Component
struct Index {
  @State timeStr : number = 1318781876;
  @State timeNow: string = ''
  build() {
    Row() {
      Column() {
        Text(`当前时间:`+this.timeNow)
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
          .width("100%")
        Button("get time")
          .onClick(() => {
            try {
              this.timeNow = this.getTimeToYYYYDDMMHHMMSS(this.timeStr)
            }
            catch (e) {
            }
          })
      }
      .justifyContent(FlexAlign.Center)
      .width('100%')
    }
    .height('100%')
  }

  getTimeToYYYYDDMMHHMMSS(str: number): string {
    let time:string =''
    time = dayjs.unix(1318781876).format('YYYY-MM-DD HH:mm:ss')
    return time
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

推荐使用三方库实现格式化,三方库的使用参考详情链接:

https://gitee.com/openharmony-tpc/openharmony_tpc_samples/tree/master/dayjs

分享
微博
QQ
微信
回复
2024-12-25 20:05:49


相关问题
HarmonyOS 时间格式化
583浏览 • 1回复 待解决
HarmonyOS 时间格式化
691浏览 • 1回复 待解决
HarmonyOS 时间格式化问题
608浏览 • 1回复 待解决
HarmonyOS 时间日期格式化
1374浏览 • 1回复 待解决
HarmonyOS 如何时间格式化
664浏览 • 1回复 待解决
HarmonyOS 如何时间进行格式化
886浏览 • 1回复 待解决
HarmonyOS DateFormat格式化
1116浏览 • 1回复 待解决
格式化console输出日志格式-美观
840浏览 • 1回复 待解决
Dev EcoStudio如何格式化代码
4279浏览 • 1回复 已解决
HarmonyOS如何实现日期格式化转换
1378浏览 • 1回复 待解决
HarmonyOS 字符串格式化
701浏览 • 1回复 待解决
保存自动格式化代码如何配置
893浏览 • 1回复 待解决
HarmonyOS 字符串格式化异常
1137浏览 • 1回复 待解决
如何关闭编辑器自动格式化
1362浏览 • 1回复 待解决
如何对电话号码进行格式化
1104浏览 • 1回复 待解决
HarmonyOS getStringSync无法格式化字符串
560浏览 • 1回复 待解决
在DevStudio中如何对代码进行格式化
1201浏览 • 1回复 待解决