React time ago not getting date

I’m using react-timeago package, i just need to get the data from a parent component by props, when i put the data in the date prop of TimeAgo i get nothing.

props.date is not getting the data in the TimeAgo component, i have tried this.props.date but in vain. the code below is in the same component

// this returns for ex 'Published 2 days ago'
<div>Published {props.date}</div>
// this returns only 'Published'
<div>Published <TimeAgo date={props.date} formatter={formatter}/></div>

Answer

Your props.date return a literal string that cannot be directly converted into a date object or date string, firstly you’ll need to convert it into a standard format and then pass it to the props. Before the return block add this code.

var calDate = stringToDate()

function stringToDate(){
 let cDate = new Date();
cDate.setDate(cDate.getDate() - parseInt(props.date)); //Get the integer part of X days ago, e.g return the integer 2 from 2 days ago and then subtract 2 days from current date.
//console.log(calDate.toString());
return cDate
}

Update the value passed to prop to the value we calculated above.

// this returns for ex 'Published 2 days ago'
<div>Published {props.date}</div>
// this returns only 'Published'
<div>Published <TimeAgo date={calDate} formatter={formatter}/></div>