I’ve a database that has a createdAt field of type date. Now I want to change the background-color of this field in my template according to it duration. That is, if it was create before one hour, it will only present #minutes with a css class of red. If it was created about before 1day #hour(s) with a blue bg-color. If it was created before a week, #days and a yellow bg-color…same for weeks and months.
Here is where I got so far in my template.onRendered
var dt = Date.now();
const results = Todos.find({}, { sort: { createdAt: -1 } });
var MS_IN_A_MONTH = 1000 * 60 * 60 * 24 * 30;
var MS_IN_A_WEEK = 1000 * 60 * 60 * 24 * 7;
var MS_IN_A_DAY = 1000 * 60 * 60 * 24;
var MS_IN_A_HOUR = 1000 * 60 * 60;
var MS_IN_A_MINUTE = 1000 * 60;
results.forEach((todos) => {
var elem = `${ todos.createdAt.toString() }`;
var difference = dt - Date.parse(elem);
console.log('parse ' + dt + 'Difference ' + difference);
var monthDifference = Math.floor(difference / MS_IN_A_MONTH);
var weekDifference = Math.floor(difference / MS_IN_A_WEEK);
var daysDifference = Math.floor(difference / MS_IN_A_DAY);
var hourDifference = Math.floor(difference / MS_IN_A_HOUR);
var minDifference = Math.floor(difference / MS_IN_A_MINUTE);
if (monthDifference >= 1 && weekDifference > 3) {
return 'label label-default';
} else if (weekDifference <= 3 && weekDifference >= 1) {
return 'label label-primary';
} else if (daysDifference < 7 && daysDifference >= 1) {
return 'label label-success';
} else if (hourDifference < 24 && hourDifference >= 1) {
return 'label label-danger';
} else if (hourDifference <= 0 && minDifference <= 1) {
return 'label label-warning';
}
});
This method is either reactive nor working. It’s simply not returning anything, Is there any better way to achieve this using reactivedict or reactive var?
I could do it using your suggestion. That was a fast great help to me. I already had the moment package installed but didn’t know you could do something like that with it. Thanks again. Look like there is no way to avoid templates helpers like suggested in this