DateTime>systemDateTime__DateDateTimeFormat_0">【HarmonyOS NEXT】DateTime>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 开始的,所以要加 1const 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}`;}
时间格式的拼接逻辑可以完全根据自己的需求进行修改。
方案二、国际化时间格式转化:
import { intl } from '@kit.LocalizationKit';// 使用系统当前locale创建DateTimeFormat对象let dateFormat = new intl.DateTimeFormat();let time = DateTime>systemDateTime.getTime();const date = new Date(time);this.mTimeContent = " 当前时间: " + dateFormat.format(date);
如果除了转换时间戳格式以外,还需要国际化的适配,需要给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"
三、DEMO示例:
import { DateTime>systemDateTime } from '@kit.BasicServicesKit'
import { intl } from '@kit.LocalizationKit';
struct TimeFormatTestPage { mTimeContent: string = "";private timeNum: number = 0;aboutToAppear(): void {// 获取当前系统时间戳let time = DateTime>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 开始的,所以要加 1const 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)}}