Typescript thinks getYear doesn't exist by Date type

I run tsc inside the webpack project, "core-js": "registry:dt/core-js#0.0.0+20160725163759" and "node": "registry:dt/node#6.0.0+20160909174046"

Other date properties work fine:

 private dateToString (date: Date) { let month = date.getMonth(); let day = date.getDate(); let year = date.getYear() + 1900; let dateString = `${month}/${day}/${year}`; return dateString; } 

Typescript perfectly recognizes date.getMonth and date.getDate , but on date.getYear it gives

Property 'getYear' does not exist on type 'Date'.

What definition am I missing?

+12
source share
2 answers

This API is deprecated. Try getFullYear() .

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear

This feature has been removed from web standards. Although some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or web applications using it can be interrupted at any time.

The getYear () method returns the year on the specified date in accordance with local time. Since getYear () does not return full years ("2000 problem"), it is no longer used and has been replaced by the getFullYear () method.

+22
source

Change this line with

 let year = date.getYear() + 1900; 

in

 let year = date.getFullYear(); 
0
source

Source: https://habr.com/ru/post/1259070/


All Articles