typescript 打字稿类型错误:......toDateString 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48976513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript TypeError: ......toDateString is not a function
提问by Novice
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. 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.
谢谢。
回答by Andrew Eisenberg
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
你想要的函数。
回答by karthiks3000
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