Novice
Novice

Reputation: 173

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. 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

Answers (3)

Guit Adharsh
Guit Adharsh

Reputation: 209

toDateString() is not a function is showing because u may used it with any variable

toDateString() is a method which can be used with a date object only

Upvotes: 0

Andrew Eisenberg
Andrew Eisenberg

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

karthiks3000
karthiks3000

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

Related Questions