Home > Blockchain >  Can I do Date calculation in React?
Can I do Date calculation in React?

Time:05-05

Say, I just retrieved an object called "food" from the database. "food" has an attribute called "created_at" and it is created automatically when food was created. "food" also has an attribute called "shelf_life" Now, in a react frontend, I want to do a conditional rendering based on whether the food is expired or not. So, I need to do some calculations.

For example,

created_at = 2022-04-20T03:25:53.763 00:00 shelf_life = 30 (days)

Is there a way to do a calculation like created_at shelf_life and compare the result to the current time?

Thanks a lot

CodePudding user response:

you can use the library moment https://momentjs.com/

and declare your two dates

let date1 = moment('2022-04-20T03:25:53.763 00:00');
let date2 = moment('2022-04-11T03:25:53.763 00:00');

then get the difference in days using

var difference = date2.diff(date1, 'days');

  • Related