admin管理员组

文章数量:1181401

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.

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.

Share Improve this question edited Feb 26, 2018 at 6:22 Andrew Eisenberg 28.7k9 gold badges96 silver badges151 bronze badges asked Feb 25, 2018 at 17:45 NoviceNovice 1731 gold badge1 silver badge7 bronze badges 1
  • Is this a compile time error ? – Titian Cernicova-Dragomir Commented Feb 25, 2018 at 17:48
Add a comment  | 

3 Answers 3

Reset to default 27

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

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

本文标签: javascriptTypescript TypeError toDateString is not a functionStack Overflow