Date类型在format的时候不能直接format

我有一个actiTime?: Date 属性,

Logger.d('Timeline', this.dateFormat.format(new Date()))   -->2024年2月4日 
Logger.d('Timeline', this.item.actiTime + "")-->1698733419786 
Logger.d('Timeline', this.dateFormat.format(this.item.actiTime))–>undefined 
Logger.d('Timeline', this.dateFormat.format(new Date(this.item.actiTime)))–> 2023年10月31日

请问这个是什么原理?

HarmonyOS
2024-05-10 21:39:54
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
lovingkane

伙伴场景中的actiTime实际上由服务端下发的json,parse之后,通过as语法关联到class,然后并非实际class实例,导致最终的actiTime并非class定义中的Date类型,依然还是json中的number类型

参考Demo如下:

class test { 
  constructor() { 
  } 
 
  a: string = ''; 
 
  doTest() { 
    console.log('doTest call'); 
  } 
} 
 
const b: test = JSON.parse(JSON.stringify({ a: 1 })) as test; 
console.log(typeof b.a); // 依然还是number 
b.doTest(); // 这一行调用会崩溃
分享
微博
QQ
微信
回复
2024-05-11 11:08:41
相关问题
DevEco Studio 不能直接更新?
7191浏览 • 8回复 已解决
new Date()获取月和日时间错误。
477浏览 • 1回复 待解决
picker-date日期组件问题?
3912浏览 • 1回复 待解决
cmake编译时候信息能不能多一些
232浏览 • 1回复 待解决
怎么进度条更新时候刷新页面?
3370浏览 • 1回复 待解决