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

devtools/2025/2/8 20:23:28/

DateTime>systemDateTime__DateDateTimeFormat_0">【HarmonyOS NEXT】DateTime>systemDateTime 时间戳转换时间格式 DateDateTimeFormat

一、前言

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

时间戳通常是一个长整型的数字,如 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)}}

http://www.ppmy.cn/devtools/157165.html

相关文章

ROS2从入门到精通3-2:详解xacro语法并优化封装urdf

0 专栏介绍 本专栏旨在通过对ROS2的系统学习,掌握ROS2底层基本分布式原理,并具有机器人建模和应用ROS2进行实际项目的开发和调试的工程能力。 🚀详情:《ROS2从入门到精通》 1 xacro文件是什么? XML 宏语言(XML Macros, Xacro)是可编程的XML文件。在xacro中可以声明变量…

自定义多功能输入对话框:基于 Qt 打造灵活交互界面

一、引言 在使用 Qt 进行应用程序开发时,我们经常需要与用户进行交互,获取他们输入的各种信息。QInputDialog 是 Qt 提供的一个便捷工具,可用于简单的输入场景,但当需求变得复杂,需要支持更多类型的输入控件&#xff0…

llama.cpp GGUF 模型格式

llama.cpp GGUF 模型格式 1. Specification1.1. GGUF Naming Convention (命名规则)1.1.1. Validating Above Naming Convention 1.2. File Structure 2. Standardized key-value pairs2.1. General2.1.1. Required2.1.2. General metadata2.1.3. Source metadata 2.2. LLM2.2.…

ORACLE 数据库的启动和关闭

文章目录 1、 启动 ORACLE 数据库的三步操作2、 关闭 ORACLE 数据库的三步操作3 、启动和关闭 ORACLE 数据库的相关命令4、 启动和关闭 ORACLE 数据库 1、 启动 ORACLE 数据库的三步操作 1、启动数据库并使它可用,有三步操作:a、启动一个实例b、装配数据…

C#元组和Unity Vector3

C#元组和Unity Vector3详解 一、C# 元组&#xff08;Tuple&#xff09; 1. 基本概念 元组是一种轻量级的数据结构可以存储多个不同类型的值C# 7.0及以后版本支持更简洁的语法支持命名和解构 2. 创建方式 // 方式1&#xff1a;使用Tuple类 Tuple<int, string> tuple1…

【安全帽头盔检测】基于YOLOV11+pytorch+Flask+SpringBoot+Vue+MySQL的安全帽头盔检测识别系统

前言 本系统是一个完整的基于YOLOV11pytorchFlaskSpringBootVueMySQL的安全帽头盔检测识别系统。 可使用YOLOV1-YOLOV11的任意模型进行目标检测。可以检测图片、视频、摄像头三种方式。能够检测出是否佩戴安全帽或头盔。可应用于工地施工现场、工厂安全检查、电瓶车头盔佩戴等…

C++ Primer 成员访问运算符

欢迎阅读我的 【CPrimer】专栏 专栏简介&#xff1a;本专栏主要面向C初学者&#xff0c;解释C的一些基本概念和基础语言特性&#xff0c;涉及C标准库的用法&#xff0c;面向对象特性&#xff0c;泛型特性高级用法。通过使用标准库中定义的抽象设施&#xff0c;使你更加适应高级…

智慧楼宇的监控都用上3D形式了,2D要摒弃了。

在信息技术飞速发展的当下&#xff0c;智慧楼宇作为现代建筑的重要发展方向&#xff0c;其监控系统也在不断升级换代。近年来&#xff0c;3D监控形式在智慧楼宇中逐渐得到广泛应用&#xff0c;引发了关于2D监控是否将被摒弃的讨论。要深入探讨这一问题&#xff0c;需从智慧楼宇…