如何获取系统时间戳以及格式的转换

遇到一个场景,点击获取系统时间按钮,如何同步获取系统时间戳,我想探究如何同步方法获取时间戳与异步方法获取时间戳,以及时间格式的转换。

HarmonyOS
2024-05-20 21:23:51
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
失望的满天星

核心代码解释

如果用同步方法获取时间戳,可以通过引入systemDateTime接口调用getTime方法获取,传参类型是boolean,false代表获取的时间戳的单位是毫秒,true代表获取的时间戳的单位是纳秒数,不过getTime方法必须要支持SDK版本是api10,手机镜像版本必须是4.1.0.30之后的版本才支持获取,如果使用异步方法,可以通过systemDateTime接口调用getCurrentTime来获取系统时间戳,然后自定义方法获取时间戳内的年月日小时分钟秒,然后进行字符串的拼接返回。

具体代码展示:

import systemDateTime from '@ohos.systemDateTime'; 
import { BusinessError } from '@ohos.base'; 
import promptAction from '@ohos.promptAction' 
import image from '@ohos.multimedia.image'; 
 
@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%') 
  } 
 
  //将时间戳转换成日期格式 
  getTimeToYYYYDDMMHHMMSS(str: number): string { 
    let time: string = ""; 
    console.log(str.toString()) 
    let date = new Date(str); 
    console.log(JSON.stringify(date)) 
    try { 
      let year = date.getFullYear(); 
      let month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1); 
      let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); 
      let hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); 
      let min = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); 
      let second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); 
      time = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + second; 
      console.log(date + "转换===>" + time); 
    } catch (e) { 
      console.info(`Failed to get currentTime. message: ${e.message}, code: ${e.code}`); 
    } 
    return time; 
  } 
}

适配的版本信息

IDE:DevEco Studio 4.0.3.600

SDK:HarmoneyOS 4.0.0.41

分享
微博
QQ
微信
回复
2024-05-21 17:17:50
相关问题
如何时间转换为日期格式时间
1006浏览 • 1回复 待解决
如何获取系统时间,你知道吗?
1204浏览 • 1回复 待解决
如何对常见密钥进行格式转换
199浏览 • 1回复 待解决
如何从C++层面获取系统时间
2724浏览 • 2回复 待解决
LiteOS-M如何获取系统当前时间
6133浏览 • 1回复 待解决
ArkTS时间获取如何实现
3033浏览 • 1回复 已解决
如何获取组件刷新时间
589浏览 • 1回复 待解决
线程信息以及线程任务栈如何获取
457浏览 • 1回复 待解决
eTS中如何进行时间与字符串转换
2563浏览 • 1回复 待解决
如何获取系统屏幕固定。
2203浏览 • 0回复 待解决
new Date()获取月和日时间错误。
470浏览 • 1回复 待解决
如何调用系统拍照并获取图片
175浏览 • 1回复 待解决