0

I have two dates.

1. the current date: new Date() // Today
2. the date when anything should expire new Date(2022, 3, 27)

I need the value between this two das (the difference is 2 days), but I need the date value from the difference between these dates so I can count -1 every second

Days| Hours | Minutes | second
 00    23        10        55

I use date-fns lib. How I make it ? can anyone help me ?

€:

Code:

  const dif = () => {
    if(expire_date) {
      const d = differenceInHours(expire_date, new Date());
      console.log(d);
    }
  };

  dif();
universe11
  • 703
  • 1
  • 11
  • 35
  • what do you mean by "count to zero with the diff value"? Could you elaborate a bit more? – cSharp Apr 25 '22 at 01:41
  • Do you have any code snippets of what you have tried so far? – Cloud Apr 25 '22 at 01:41
  • @cSharp , Maybe a User add a image in two days, so in the notification should be a timer how long still it takes, a timer that count every time -1 second. – universe11 Apr 25 '22 at 01:45
  • @Cloud yes I edited my code, I only get the value 2 , it make sense then between my two dates is a difference between 2 days, but I need a date value, to count -1 from this difference days – universe11 Apr 25 '22 at 01:45
  • So do you want to create a [countdown timer](https://stackoverflow.com/questions/20618355/how-to-write-a-countdown-timer-in-javascript)? – cSharp Apr 25 '22 at 01:48
  • @cSharp yes exaclty, but with the value from today and maybe the 27th. – universe11 Apr 25 '22 at 01:50
  • 1
    Yup, you have already figured out a way to get the difference in the 2 dates, right? Use the function in the linked thread and put in the required value accordingly. – cSharp Apr 25 '22 at 01:52
  • @cSharp sorry I was overthinking it... thank you^^ – universe11 Apr 25 '22 at 01:53
  • 1
    I think this is easy with https://www.npmjs.com/package/moment – RamaProg Apr 25 '22 at 01:56

2 Answers2

0

you can try something like this

const secTimer = setInterval(() => {
        const msDiff =
          new Date(item.discount_date).getTime() - new Date().getTime();
        const daysDiff = Math.floor(msDiff / (1000 * 60 * 60 * 24));
        const hrsDiff = Math.floor(
          (msDiff - daysDiff * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
        );
        const mmDiff = Math.floor(
          (msDiff -
            daysDiff * (1000 * 60 * 60 * 24) -
            hrsDiff * (1000 * 60 * 60)) /
            (1000 * 60)
        );
        const ssDiff = Math.floor(
          (msDiff -
            daysDiff * (1000 * 60 * 60 * 24) -
            hrsDiff * (1000 * 60 * 60) -
            mmDiff * (1000 * 60)) /
            1000
        );
        setDay(daysDiff);
        setHrs(hrsDiff);
        setMm(mmDiff);
        setSs(ssDiff);
   
    }, 1000);

use usestate for day, hrs, min, secs.

Hari Prasad
  • 461
  • 1
  • 3
  • 9
0

You can use monentjs

moment('2019-05-11').isSame('2019-05-11','day'); // true

moment('2019-05-11').isSame('2019-05-12','day'); // false

"Compare two dates in JavaScript using moment.js - Poopcode" https://poopcode.com/compare-two-dates-in-javascript-using-moment/amp/

I use moment both for react and nodejs

Read all about it and the different advanced functions in the api guide "Moment.js | Guides" https://momentjs.com/guides/

Ziv Adler
  • 169
  • 2
  • 11
  • Worth noting that Moment.js is now a legacy project, and it may be better to use other more modern alternatives – Cloud Apr 25 '22 at 02:57
  • Yea but it is working great and moment anyway is just to show how to make it simple and clean.. You can use other similar libraries to moment... Just like juda for example – Ziv Adler Apr 26 '22 at 05:16