HarmonyOS 时间戳格式化

如下截图,我在尝试使用calendar进行时间戳格式化,calendar.setTime之后,获取的时间戳不是我设置的请问针对这种格式化是否有建议方案

HarmonyOS 时间戳格式化 -鸿蒙开发者社区

HarmonyOS
2024-12-25 17:16:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

目前HarmonyOS 没有直接格式化的api,针对时间戳格式化现有两种解决方案:

第一种:自定义方法获取时间戳内的年月日小时分钟秒,然后进行字符串的拼接返回。完整代码如下:

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%') }

  //将时间戳转换成日期格式
  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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.

第二种使用第三方库dayjs,完整代码如下:

import dayjs from "dayjs";

@Entry
@Component
struct Index {
  @State timeStr : number = 1318781876;
  @State timeNow: string = ''
  build() {
    Row() {
      Column() {
        Text(`当前时间:`+this.timeNow)
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
          .width("100%")
        Button("get time")
          .onClick(() => {
            try {
              this.timeNow = this.getTimeToYYYYDDMMHHMMSS(this.timeStr)
            }
            catch (e) {
            }
          })
      }
      .justifyContent(FlexAlign.Center)
      .width('100%')
    }
    .height('100%')
  }

  getTimeToYYYYDDMMHHMMSS(str: number): string {
    let time:string =''
    time = dayjs.unix(1318781876).format('YYYY-MM-DD HH:mm:ss')
    return time
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

三方库的下载使用参考链接:

https://gitee.com/openharmony-tpc/openharmony_tpc_samples/tree/master/dayjs

分享
微博
QQ
微信
回复
2024-12-25 19:41:00


相关问题
HarmonyOS 如何将时间格式化
664浏览 • 1回复 待解决
HarmonyOS 时间格式化
582浏览 • 1回复 待解决
HarmonyOS 时间格式化问题
607浏览 • 1回复 待解决
HarmonyOS 时间日期格式化
1373浏览 • 1回复 待解决
HarmonyOS 如何获取格式化时间
577浏览 • 1回复 待解决
HarmonyOS 如何将时间进行格式化
886浏览 • 1回复 待解决
HarmonyOS DateFormat格式化
1116浏览 • 1回复 待解决
格式化console输出日志格式-美观
840浏览 • 1回复 待解决
HarmonyOS 字符串格式化
701浏览 • 1回复 待解决
Dev EcoStudio如何格式化代码
4279浏览 • 1回复 已解决
如何将时间转换为日期格式时间
3899浏览 • 1回复 待解决
HarmonyOS 字符串格式化异常
1137浏览 • 1回复 待解决
HarmonyOS如何实现日期格式化转换
1378浏览 • 1回复 待解决
HarmonyOS getStringSync无法格式化字符串
560浏览 • 1回复 待解决
保存自动格式化代码如何配置
892浏览 • 1回复 待解决
HarmonyOS 时间时间
664浏览 • 1回复 待解决