Reputation: 305
So I am calling my firestore database and trying to return the date part from a timestamp but it is returning the wrong year.
<text>{new Date(data.value * 1000).toLocaleDateString('en-GB')}</text>
I am aware the 1000 is to with seconds but without it returns invalid date.
getjobs = async () => {
try {
const unsubscribe = await firebase.firestore().collection("jobs")
.onSnapshot((querySnapshot) => {
var _jobs = [];
querySnapshot.forEach( (doc)=> {
const job = doc.data();
if(job.company === this.state.user.company){
_jobs.push({ ...job, id: doc.id });
}
});
console.log('---------state',this.state);
this.setState({
jobs: _jobs,
})
});
this.setState({
unsubscribe
})
} catch (e) {
console.log('error cant get jobs', e);
}
}
List item
Item = (data, index) => {
return (
<ListItem thumbnail key={index} style={styles.listItem}>
<Body>
<Text>Job Number: {data.jobnumber}</Text>
<Text note numberOfLines={1}>Trade: {data.trade}</Text>
<Text note numberOfLines={1}>PostCode: {data.postcode}</Text>
<Text note numberOfLines={1}>Date: {new Date(data.value * 1000).toLocaleDateString('en-GB')}</Text>
</Body>
<Right>
<FontAwesome5 name="chevron-right" size={22} color="#f53025" />
</Right>
</ListItem>
);
}
Upvotes: 2
Views: 708
Reputation: 305
I didn't realize one of my documents within my collection didn't have a date field and that's why it was giving me an error.
{data.value.toDate().toLocaleDateString('en-GB')}
Upvotes: 2