P
P
PlasterTom2018-03-10 00:53:09
JavaScript
PlasterTom, 2018-03-10 00:53:09

How to get year from date (problem with getFullYear)?

This code console.log( this.props.dateRange)outputs:
5aa30116115c4714623907.jpegdateRange- an object,
this.props.dateRange.start- also an object,
why can't I output this.props.dateRange.start.getFullYear?
Throws an error cannot read property 'getfullyear' of undefined.
What is the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2018-03-10
@alexey-m-ukolov

You most likely have a lifecycle issue. Perhaps the props are dynamically updated after the component is mounted, or something similar happens. The problem is not that you can't get the year, but that instead of dateRange you get an empty object (or an object that does not have a start property or it does, but is undefined).
Understand the lifecycle of your component and add appropriate validation checks to its props.

P
Pavel K, 2018-03-10
@PavelK

Throws an error cannot read property 'getfullyear' of undefined.
Because it's a feature...
console.log( this.props.dateRange.start.getFullYear() );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question