Reputation: 151
function NoticeList({ title, dates }) {
let today = new Date();
let year = today.getFullYear();
let month = today.getMonth() + 1;
let date = today.getDate();
let formetToday = `${year}-${month < 10 ? `0${month}` : month}-${
date < 10 ? `0${date}` : date
} `;
let formetDates = moment(dates).format('YYYY-MM-DD');
console.log(date && formetToday === formetDates);
return (
<NoticeListContainer>
<NoticeLists>
{formetDates === formetToday ? <div>New</div> : null}
<h5>{title}</h5>
<div>{formetDates}</div>
</NoticeLists>
</NoticeListContainer>
);
}
export default NoticeList;
I would like to receive the data of the announcement and compare the date of the announcement with today's date to print out the new phrase. However, after comparing it with console.log, it all shows false. What's the reason? And if there's any other good way than this, please let me know.
Upvotes: 2
Views: 142
Reputation: 148
Have you checked what the formetToday variable looks like? The formatting of the string seems weird and causes errors.
This returns true (if the date is indeed 2020-09-18)
let today = new Date();
let year = today.getFullYear();
let month = today.getMonth() + 1;
let date = today.getDate();
let formetToday = `${year}-${month < 10 ? "0"+month : month}-${date < 10 ? "0"+date : date}`;
let formetDates = moment("2020-09-18").format('YYYY-MM-DD');
console.log(date && formetToday === formetDates);
Upvotes: 1