Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 744
Novice Avatar asked Feb 25 '18 17:02

Novice


People also ask

What is toDateString?

The toDateString() method returns the date portion of a Date object interpreted in the local timezone in English.

How do you format Todatest?

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.

How do I use JavaScript todate?

JavaScript toDateString() MethodThe toDateString() method returns the date (not the time) of a date object as a string.


2 Answers

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.

like image 108
Andrew Eisenberg Avatar answered Oct 25 '22 17:10

Andrew Eisenberg


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

like image 45
karthiks3000 Avatar answered Oct 25 '22 17:10

karthiks3000