Ahmed Ibrahim
Ahmed Ibrahim

Reputation: 517

How do I subtract date from today's date in React?

The logic I want to display is:

{transfusions.map((transfusion) => {
          return (
            <tr key={transfusion.id} className="shadow-lg">
              <th scope="row">{transfusion.id}</th>
              <td>{transfusion.donor}</td>
              <td>{transfusion.blood_component}</td>
              <td>{transfusion.unit}</td>
              <td>{transfusion.created_at}</td>
              <td>
                <i
                  className="fas fa-edit"
                  onClick={() => handleUpdate(transfusion)}
                ></i>{" "}
                <i
                  className="fas fa-trash pl-1"
                  onClick={() =>
                    window.confirm(
                      "Are you sure you wish to delete this transfusion?"
                    ) && deleteTransfusion(transfusion.id)
                  }
                ></i>
              </td>
            </tr>

created_at date is displaying as this format 2020-07-15T13:29:15.524486Z

So I want to subtract the current/today's date which is like this format Wed Jul 22 2020 10:57:59 GMT+0300 (East Africa Time)

and finally display the number of days between these dates which is like integer number 13

Upvotes: 0

Views: 6563

Answers (2)

laruiss
laruiss

Reputation: 3816

You can use the Date constructor:

const currentDate = new Date('Wed Jul 22 2020 10:57:59 GMT+0300 (East Africa Time)')
const oldDate = new Date('2020-07-15T13:29:15.524486Z')
currentDate - oldDate // 584923476

To display the duration, you can either use date-fns, luxon, moment or Temporal

Upvotes: 0

Rubens
Rubens

Reputation: 366

const daysBetween = new Date().getDate() - new Date('2020-07-15T13:29:15.524486Z').getDate()

Upvotes: 1

Related Questions