-1

My goal is to make a time countdown component. I must show a difference of date from current date to user-entered date, in Year Month Day Hour Min Sec format.

I tried to minus the two dates but ended up with 00:00:00:00. Adding them gave me a result but minus gives me this 00.

Other answers do not satisfy requirements or explain why I get a difference of 0 when subtracting dates.

my code:

import "./App.css";
import Countdown from "react-countdown";

function App() {
  console.log(Date.now());

  return (
    <div className="App">
      <h1>Hello</h1>
      <form>
        <input type="date" />
        <button>submit</button>
      </form>
      <Countdown date={new Date(2021, 6, 10) + Date.now()} />,
    </div>
  );
}
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
Sai Krishnadas
  • 2,863
  • 9
  • 36
  • 69
  • Does this answer your question? [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – tevemadar Jun 04 '21 at 14:28
  • @tevemadar Nope. – Sai Krishnadas Jun 04 '21 at 14:29
  • Sorry, wrong paste. This is the one: https://stackoverflow.com/questions/7763327/how-to-calculate-date-difference-in-javascript – tevemadar Jun 04 '21 at 14:30
  • Does this answer your question? [How to calculate date difference in JavaScript?](https://stackoverflow.com/questions/7763327/how-to-calculate-date-difference-in-javascript) – Anurag Dabas Jun 05 '21 at 02:02

2 Answers2

0

You can either do it manually as

function addDays(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

or use moment js ( https://momentjs.com/ ) for operations

Noel
  • 88
  • 7
0

using moment.js, you can do it in this way:

const diffDates = (start, end) => {
  let a = moment(start);
  let b = moment(end);
  return a.diff(b, "days");
};

faramarz razmi
  • 279
  • 1
  • 7