[英]Typescript TypeError: ......toDateString is not a function
I am new to typescript and this might be something very trivial.我是打字稿的新手,这可能是一件非常微不足道的事情。 Please help.
请帮忙。
I am trying to extract minutes and seconds from typescript Date variable.我正在尝试从 typescript Date 变量中提取分钟和秒。 I have a variable timestamp declared as Date.
我有一个变量时间戳声明为日期。 But when I try to use functions on it like toDateString() or getMinutes() or getTime(), I get the above error saying TypeError: timestamp.getMinutes() is not a function.
但是当我尝试在它上面使用函数时,比如 toDateString() 或 getMinutes() 或 getTime(),我得到上面的错误说 TypeError: timestamp.getMinutes() is not a function。
Thanks.谢谢。
It's likely that your timestamp is not a date, just a number that represents the number of milliseconds since the Unix epoch .您的时间戳很可能不是日期,只是一个表示自 Unix epoch 以来的毫秒数的数字。
All you need to do is convert your timestamp to a Date, like this:您需要做的就是将时间戳转换为日期,如下所示:
let currentDate = new Date(timestamp);
Then you can call any Date
functions you want on it.然后你可以调用任何你想要的
Date
函数。
You can use the Angular DatePipe to achieve this-您可以使用 Angular DatePipe 来实现这一点 -
{{current_date | date: 'shortTime'}}
You can find more information here - https://angular.io/api/common/DatePipe您可以在此处找到更多信息 - https://angular.io/api/common/DatePipe
toDateString() is a method which can be used with a date object only toDateString() 是一种只能与日期对象一起使用的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.