HarmonyOS 有秒级时间戳 ,怎么根据传入的fortmatStr 输出时间字符串?

有秒级时间戳 ,怎么根据传入的fortmatStr 输出时间字符串?fortmatStr 可以自定义,比如yyyy-MM-dd

HarmonyOS
2024-08-29 11:27:57
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

可以参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/i18n-time-date-V5#时间日期和相对时间格式化

参考这个demo

import systemDateTime from '@ohos.systemDateTime'; 
import { BusinessError } from '@ohos.base'; 
 
@Entry 
@Component 
struct Index5 { 
 @State timeStr: string = ''; 
 
 build() { 
  Flex( 
   { direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
   Text(`当前时间:` + this.timeStr) 
    .fontSize(20) 
    .fontWeight(FontWeight.Bold) 
   Button("get time") 
    .width(100) 
    .height(100).onClick(() => { 
    //同步方法获取系统时间戳 
    try { 
     let time = systemDateTime.getTime(false) 
     this.timeStr = this.getTimeToYYYYDDMMHHMMSS(time) 
    } catch (e) { 
     let error = e as BusinessError; 
     console.info(`Failed to get time. message: ${error.message}, code: ${error.code}`); 
    } 
    //异步方法获取系统时间戳 
    try { 
     systemDateTime.getCurrentTime(false, (error, time) => { 
      if (error) { 
       console.info(`Failed to get currentTime. message: ${error.message}, code: ${error.code}`) 
       return; 
      } 
      console.info(`Succeeded in getting currentTime : ${time}`); 
      this.timeStr = this.getTimeToYYYYDDMMHHMMSS(time) 
     }) 
     AlertDialog.show({ message: 'sss' }); 
    } catch (e) { 
     console.info(`Failed to get currentTime. message: ${e.message}, code: ${e.code}`); 
    } 
   }) 
  } 
  .width('100%') 
  .height('100%') 
 }
分享
微博
QQ
微信
回复
2024-08-29 16:48:05
相关问题
HarmonyOS 时间运用
135浏览 • 1回复 待解决
HarmonyOS 转换时间方法
90浏览 • 1回复 待解决
如何获取当前系统时间时间
370浏览 • 1回复 待解决
eTS中如何进行时间字符串转换?
3857浏览 • 1回复 待解决
如何将时间转换为日期格式时间
2345浏览 • 1回复 待解决
如何获取今天日期、时间
190浏览 • 1回复 待解决
HarmonyOS 字符串怎么手动加换行?
94浏览 • 1回复 待解决
HarmonyOS字符串替换问题
199浏览 • 1回复 待解决
检查字符串是否以给定字符串开头
180浏览 • 1回复 待解决
检查字符串是否以给定字符串结尾
150浏览 • 1回复 待解决
HarmonyOS $r 字符串替换问题
173浏览 • 1回复 待解决
如何获取系统时间以及格式转换
1221浏览 • 1回复 待解决
HarmonyOS 如何遍历包含emoji字符串
105浏览 • 1回复 待解决