HarmonyOS 时间格式转换问题

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

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

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

ohpm install dayjs  

可参考以下文档: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%')
  }
}
分享
微博
QQ
微信
回复
2025-01-10 11:09:02
相关问题
如何将时间转换为日期格式时间
3768浏览 • 1回复 待解决
HarmonyOS 时间格式问题
471浏览 • 1回复 待解决
如何获取系统时间戳以及格式转换
2589浏览 • 1回复 待解决
HarmonyOS 时间转换
453浏览 • 1回复 待解决
HarmonyOS 音频编码格式转换
244浏览 • 1回复 待解决
HarmonyOS 时间戳如何转换时间
326浏览 • 1回复 待解决
HarmonyOS 时间格式
409浏览 • 1回复 待解决
HarmonyOS 时间格式
456浏览 • 1回复 待解决
HarmonyOS 转换时间戳的方法
964浏览 • 1回复 待解决
HarmonyOS timestamp转换为正常时间
393浏览 • 1回复 待解决
HarmonyOS 时间日期格式
1234浏览 • 1回复 待解决
HarmonyOS 加密字符串格式转换
467浏览 • 1回复 待解决
HarmonyOS如何实现日期格式转换
1263浏览 • 1回复 待解决
HarmonyOS 如何获取格式化的时间
442浏览 • 1回复 待解决
HarmonyOS 有无YUV格式转换png的api
316浏览 • 1回复 待解决
HarmonyOS 日期时间与字符串转换
833浏览 • 1回复 待解决
如何对常见密钥进行格式转换
971浏览 • 1回复 待解决
如何将某个时间转换成距现在的时间
867浏览 • 1回复 待解决
HarmonyOS 如何将时间进行格式化?
767浏览 • 1回复 待解决
HarmonyOS 如何将时间格式
522浏览 • 1回复 待解决