HarmonyOS 时间格式转换问题

如何将接口返回的字符串时间格式:20240707103952转化为yyyy-MM-dd HH:mm:ss格式。

HarmonyOS
2025-01-10 08:04:34
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

可以使用三方库dayjs,下载安装:

ohpm install dayjs  
  • 1.

可参考以下文档:https://gitee.com/openharmony-tpc/openharmony_tpc_samples/tree/master/dayjs

示例参考如下:

import dayjs from "dayjs";

@Entry
@Component
struct Index {
  @State timeStr: number = 20240707103952;
  @State timeNow: string = ''

  build() {
    Row() {
      Column() {
        Text('当前时间:' + this.timeNow).fontSize(20).fontWeight(FontWeight.Bold).width("100%")
        Button("get time").onClick(() => {
          try {
            this.timeNow = dayjs.unix(this.timeStr).format('YYYY-MM-DD HH:mm:ss')
          } catch (e) {
          }
        })
      }.justifyContent(FlexAlign.Center).width('100%')
    }.height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
分享
微博
QQ
微信
回复
2025-01-10 11:09:02
相关问题
如何将时间转换为日期格式时间
4322浏览 • 1回复 待解决
HarmonyOS 时间格式问题
848浏览 • 1回复 待解决
如何获取系统时间戳以及格式转换
3125浏览 • 1回复 待解决
HarmonyOS 时间转换
873浏览 • 1回复 待解决
HarmonyOS 音频编码格式转换
615浏览 • 1回复 待解决
HarmonyOS 时间戳如何转换时间
653浏览 • 1回复 待解决
HarmonyOS 时间格式
910浏览 • 1回复 待解决
HarmonyOS 时间格式
1101浏览 • 1回复 待解决
HarmonyOS 转换时间戳的方法
1288浏览 • 1回复 待解决
HarmonyOS timestamp转换为正常时间
701浏览 • 1回复 待解决
HarmonyOS 加密字符串格式转换
811浏览 • 1回复 待解决
HarmonyOS 时间日期格式
1689浏览 • 1回复 待解决
HarmonyOS 有无YUV格式转换png的api
654浏览 • 1回复 待解决
HarmonyOS如何实现日期格式转换
1656浏览 • 1回复 待解决
HarmonyOS 如何获取格式化的时间
852浏览 • 1回复 待解决
如何对常见密钥进行格式转换
1267浏览 • 1回复 待解决
HarmonyOS 日期时间与字符串转换
1779浏览 • 1回复 待解决
如何将某个时间转换成距现在的时间
1217浏览 • 1回复 待解决
HarmonyOS 如何将时间进行格式化?
1172浏览 • 1回复 待解决
HarmonyOS 如何将时间格式
983浏览 • 1回复 待解决