【HarmonyOS NEXT】systemDateTime 时间戳转换为时间格式 Date,DateTimeFormat 原创

George_wu_
发布于 2025-3-24 22:50
浏览
0收藏

【HarmonyOS NEXT】systemDateTime 时间戳转换为时间格式 Date,DateTimeFormat

一、前言

在鸿蒙应用开发中,经常需要将时间戳转化为标准时间格式。即:一串数字转化为年月日时分秒。

时间戳通常是一个长整型的数字,如 1630416000000,对于普通用户来说,这个数字没有实际的意义,他们难以从中获取到有用的时间信息。

而将其转换为常见的时间格式,如 2021 - 09 - 01 00:00:00,用户可以直观地了解到具体的日期和时间,极大地提升了信息的可读性。

因为鸿蒙应用开发使用ArkTS包含于TypeScript语言,所以我们可以通过传统的Date对象解析进行时间戳转化时间格式的处理。

不过在鸿蒙系统API中,提供了用于国际化时间格式转化的接口。该接口根据不同的语言,进行了时间格式显示的处理。例如中国人喜欢从左到右 2021 - 09 - 01 。外国人某些场景下,习惯于另外的展示效果:Friday, 17 December 2021 at 03:24:00

二、解决方案:

方案根据场景需求进行选择:

方案一,Date对象解析:

  private formatTimestamp(timestamp: number): string {
    // 创建一个 Date 对象,将时间戳转换为日期时间
    const date = new Date(timestamp);
    // 获取年份
    const year = date.getFullYear();
    // 获取月份,注意 getMonth() 返回的月份是从 0 开始的,所以要加 1
    const month = String(date.getMonth() + 1).padStart(2, '0');
    // 获取日期
    const day = String(date.getDate()).padStart(2, '0');
    // 获取小时
    const hours = String(date.getHours()).padStart(2, '0');
    // 获取分钟
    const minutes = String(date.getMinutes()).padStart(2, '0');
    // 获取秒数
    const seconds = String(date.getSeconds()).padStart(2, '0');

    // 按照 'YYYY-MM-DD HH:mm:ss' 的格式拼接时间字符串
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

时间格式的拼接逻辑可以完全根据自己的需求进行修改。

方案二、国际化时间格式转化:

import { intl } from '@kit.LocalizationKit';

    // 使用系统当前locale创建DateTimeFormat对象
    let dateFormat = new intl.DateTimeFormat();
    let time = systemDateTime.getTime();
    const date = new Date(time);
    this.mTimeContent = " 当前时间: "  + dateFormat.format(date);

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

如果除了转换时间戳格式以外,还需要国际化的适配,需要给DateTimeFormat初始化进行配置:

let date = new Date(2021, 11, 17, 3, 24, 0); // 时间日期为2021.12.17 03:24:00
// 使用 en-GB locale创建DateTimeFormat对象
let datefmt = new intl.DateTimeFormat("en-GB");
let formattedDate = datefmt.format(date); // formattedDate "17/12/2021"

// 使用 en-GB locale创建DateTimeFormat对象,dateStyle设置为full,timeStyle设置为medium
datefmt = new intl.DateTimeFormat("en-GB", { dateStyle: 'full', timeStyle: 'medium' });
formattedDate = datefmt.format(date); // formattedDate "Friday, 17 December 2021 at 03:24:00"
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

三、DEMO示例:

import { systemDateTime } from '@kit.BasicServicesKit'
import { intl } from '@kit.LocalizationKit';


@Entry
@Component
struct TimeFormatTestPage {

  @State mTimeContent: string = "";
  private timeNum: number = 0;

  aboutToAppear(): void {
    // 获取当前系统时间戳
    let time = systemDateTime.getTime();
    this.timeNum = time;
    this.mTimeContent = " 当前时间戳: " + time;
  }

  onChangeTimeFormat = ()=>{
    // 方案一,Date对象解析:
    this.mTimeContent = " 当前时间: " + this.formatTimestamp(this.timeNum);

    // 方案二、国际化时间格式转化:
    // // 使用系统当前locale创建DateTimeFormat对象
    // let dateFormat = new intl.DateTimeFormat();
    // const date = new Date(this.timeNum);
    // this.mTimeContent = " 当前时间: "  + dateFormat.format(date);
  }

  private formatTimestamp(timestamp: number): string {
    // 创建一个 Date 对象,将时间戳转换为日期时间
    const date = new Date(timestamp);
    // 获取年份
    const year = date.getFullYear();
    // 获取月份,注意 getMonth() 返回的月份是从 0 开始的,所以要加 1
    const month = String(date.getMonth() + 1).padStart(2, '0');
    // 获取日期
    const day = String(date.getDate()).padStart(2, '0');
    // 获取小时
    const hours = String(date.getHours()).padStart(2, '0');
    // 获取分钟
    const minutes = String(date.getMinutes()).padStart(2, '0');
    // 获取秒数
    const seconds = String(date.getSeconds()).padStart(2, '0');

    // 按照 'YYYY-MM-DD HH:mm:ss' 的格式拼接时间字符串
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }

  build() {
    Column() {
      Text(this.mTimeContent)
        .fontSize(52)
        .fontColor(Color.Black)
        .onClick(this.onChangeTimeFormat)
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }

}
  • 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.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
收藏
回复
举报
回复
    相关推荐