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.
The toDateString() method returns the date portion of a Date object interpreted in the local timezone in English.
The toDateString() Method in JavaScript First three letters of the week day name. First three letters of the month name. Two digit day of the month, padded on the left a zero if necessary. Four digit year (at least), padded on the left with zeros if necessary.
JavaScript toDateString() MethodThe toDateString() method returns the date (not the time) of a date object as a string.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With