请问HarmonyOS next有没有同步获取当前时间戳的方法?

有没有同步获取当前时间戳的方法,还是说只能用systemDateTime.getCurrentTime异步获取

阅读 739
1 个回答

你可以使用Date.now()方法获取,以下是demo

@Entry 
@Component 
struct Index22 { 
  @State formattedDate: string = ''; 
 
  aboutToAppear(): void { 
 
    let dateFormat = new Intl.DateTimeFormat('zh-CN', {dateStyle: 'short', timeStyle: 'short'}); 
    this.formattedDate = dateFormat.format(Date.now()) 
 
    console.log( this.formattedDate) 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.formattedDate) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题