HarmonyOS 元服务中获取时间?

systemDateTime在元服务中不能用。

元服务中systemDateTime.getTime()怎么获取这种时间的呢?

阅读 692
1 个回答

demo参考:

import { authentication } from '@kit.AccountKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  build() {
    Column() {
      Button('test')
        .onClick(() => {
          let timestamp1 = this.getDateStringWithTimeStamp(1719283655000)
          let timestamp2 = this.getDateStringWithTimeStamp(1719995386863)
          console.info('timestamp1', timestamp1)
          console.info('timestamp2', timestamp2)
          //获取当前时间
          let time = new Date().getTime()
          console.log("时间是:" + time)
          console.log("时间是:" + this.getDateStringWithTimeStamp(time))
        })
    }

  }

  getDateStringWithTimeStamp(timestamp: number): string {
    let date = new Date(timestamp);
    const year = date.getFullYear();
    const month = ("0" + (date.getMonth() + 1)).slice(-2);
    const day = ("0" + date.getDate()).slice(-2);
    const hour = date.getHours();
    const min = date.getMinutes()
    const sec = date.getSeconds()
    // let formattedDate = `${year}年${month}月${day}日`
    let formattedDate = `${year}年${month}月${day}日${hour}时${min}分${sec}秒`
    return formattedDate
  }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进