Reputation: 173
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.
Thanks.
Upvotes: 16
Views: 42602
Reputation: 209
toDateString() is a method which can be used with a date object only
Upvotes: 0
Reputation: 28757
It's likely that your timestamp is not a date, just a number that represents the number of milliseconds since the 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.
Upvotes: 28
Reputation: 912
You can use the Angular DatePipe to achieve this-
{{current_date | date: 'shortTime'}}
You can find more information here - https://angular.io/api/common/DatePipe
Upvotes: 1