0

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

Ahmed Ibrahim
  • 477
  • 1
  • 7
  • 18
  • 1
    Does this answer your question? [How do I get the difference between two Dates in JavaScript?](https://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript) – mutantkeyboard Jul 22 '20 at 08:06
  • 1
    no bro and thanks for your suggestion. – Ahmed Ibrahim Jul 22 '20 at 08:39
  • And why not? While the accepted answer is not really useful, the second most voted one does exactly what you need: https://stackoverflow.com/a/15810692/7916438 – tevemadar Jul 22 '20 at 14:12

2 Answers2

1
const daysBetween = new Date().getDate() - new Date('2020-07-15T13:29:15.524486Z').getDate()
Rubens
  • 341
  • 1
  • 6
  • Thanks. This answer really helped me with my issue. Thanks again. – Ahmed Ibrahim Jul 22 '20 at 08:33
  • 1
    @AhmadEbrahim this answer is wrong. `getDate()` simply returns the day part of a date. Try it with a couple dates from June for example. – tevemadar Jul 22 '20 at 08:42
  • yea you're right, it would not work as intended. The solution @laruiss gave coupled with your comment would give the correct result. – Rubens Jul 22 '20 at 08:48
0

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

laruiss
  • 3,780
  • 1
  • 18
  • 29
  • Thanks for your answer. can you convert me these numbers 584923476 into understandable numbers like 20days, 46, days, 855day? – Ahmed Ibrahim Jul 22 '20 at 08:13
  • 2
    @AhmadEbrahim that number is expressed in milliseconds. In order to have days, divide it by `(1000*60*60*24)`. – tevemadar Jul 22 '20 at 08:44