HarmonyOS 鸿蒙Next 日期获取不准确,API9,DevEco Studio 3.1.1 Release
HarmonyOS 鸿蒙Next 日期获取不准确,API9,DevEco Studio 3.1.1 Release
// 获取当前日期的年、月、日
getCurrentDate(): string {
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = ("0" + (currentDate.getMonth() + 1)).slice(-2); // 月份是从0开始计数的,所以需要加1
const day = ("0" + currentDate.getDate()).slice(-2); // 为了保证始终是两位数,不足两位前面补0
return `${year}-${month}-${day}`;
}
let currDate = this.getCurrentDate();
console.log('MainPage---currDate', currDate)
Log日志:
05-29 20:08:32.669 5950-21750/com.huawei.targetmanagement I 0FEFE/JsApp: MainPage---currDate 2024-05-29
win11系统时间, 时间不匹配 ?
更多关于HarmonyOS 鸿蒙Next 日期获取不准确,API9,DevEco Studio 3.1.1 Release的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
getLocalDateTime(date?: Date): void {
date = date || new Date();
const formatter = new Intl.DateTimeFormat("zh-CN", {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
weekday: 'long' // 获取完整的星期名称
});
let formatCurrDate = formatter.format(date);
console.log("MainPage---formatCurrDate", formatCurrDate);
}
输出结果, 还是不对呀 ?
05-29 23:44:46.200 29585-28758/? I 0FEFE/JsApp: MainPage---formatCurrDate 2024年05月29日星期三 15:44:46
还有为啥前边Log日志的时间也是不对的 ?
更多关于HarmonyOS 鸿蒙Next 日期获取不准确,API9,DevEco Studio 3.1.1 Release的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
试试new Intl.DateTimeFormat()
参考:https://developer.huawei.com/consumer/cn/blog/topic/03152893501345021
在HarmonyOS(鸿蒙系统)中,日期获取不准确的问题可能与系统API的调用方式或时区设置有关。API 9版本中,获取日期的常用方法是使用ohos.systemDateTime
模块中的getCurrentTime
函数。如果日期不准确,检查设备时区设置是否正确,确保设备时间与网络时间同步。另外,DevEco Studio 3.1.1 Release版本可能存在与日期获取相关的已知问题,建议查阅更新日志或官方文档以确认是否有相关修复。如果问题持续,可以尝试使用ohos.systemDateTime
模块中的其他函数,如getTimeZone
和setTimeZone
,以进一步验证和调整时区设置。
在HarmonyOS鸿蒙Next中使用API 9时,如果发现日期获取不准确,可能是由于系统时间设置或API调用方式的问题。建议首先检查设备的系统时间是否正确,并确保时区设置无误。其次,确认使用的API是 @ohos.systemDateTime
模块中的 getCurrentTime
方法,并正确处理返回的时间戳。如果问题依然存在,可以尝试更新DevEco Studio到最新版本,或查看官方文档和社区是否有相关已知问题和解决方案。