2

I have an event, this events date and time is formatted to be of type unix timestamp.

In JavaScript (not looking for a plugin approach, such as modern.js), I'm now trying to determine the time difference between todays date and the event date.

I'm looking to implement a "countdown to event" feature which will countdown the days, hours, minutes and seconds to the event date.

{{ event_date_and_time }} in my code below is a HuBL variable.

Current approach:

var todays_date = new Date().getTime() / 1000;
  var event_date = new Date({{ event_date_and_time }}).getTime() / 1000

  var start = todays_date;
  var end = event_date;

  console.log(todays_date); // this logs "1596626215.983"
  console.log(end); // this logs "1596888000"

  function timeDifference(date1,date2) {
      var difference = date1 - date2;

      var daysDifference = Math.floor(difference/1000/60/60/24);
      difference -= daysDifference*1000*60*60*24

      var hoursDifference = Math.floor(difference/1000/60/60);
      difference -= hoursDifference*1000*60*60

      var minutesDifference = Math.floor(difference/1000/60);
      difference -= minutesDifference*1000*60

      var secondsDifference = Math.floor(difference/1000);

      console.log('difference = ' +
        daysDifference + ' day/s ' +
        hoursDifference + ' hour/s ' +
        minutesDifference + ' minute/s ' +
        secondsDifference + ' second/s ');
  }

  timeDifference(start, end);

So, the time of my test was 12:15pm (todays_date). The event date is set to 8th August 12:00pm.

The results I'm getting with the above is:

difference = -1 day/s 23 hour/s 55 minute/s 38 second/s

Which is obviously wrong, where am I going wrong?

Freddy
  • 683
  • 4
  • 35
  • 114
  • `difference = date2 - date1` - if you kept with `start`/`end` instead of `1`/`2` it would be clearer – freedomn-m Aug 05 '20 at 11:33
  • Why are you dividing the time by 1000? why not get the exact time? – Stender Aug 05 '20 at 11:36
  • 1
    Does this answer your question? [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – freedomn-m Aug 05 '20 at 11:39

1 Answers1

3

Just removed the division by 1000, and set a date,

Seems to be working as expected.

My guess is that, the division by 1000, actually made both the dates render as in 1970, where unix timestamps begins since neither

"1596626215.983"

Or

"1596888000"

Are actual dates in Unix and is both essentially Errors

var todays_date = new Date().getTime();
  var event_date = new Date('August 08, 20 00:20:18 GMT+00:00').getTime();

  var start = todays_date;
  var end = event_date;

  console.log(todays_date); // this logs "1596626215.983"
  console.log(end); // this logs "1596888000"

  function timeDifference(date1,date2){
      var difference = date1 - date2;

      var daysDifference = Math.floor(difference/1000/60/60/24);
      difference -= daysDifference*1000*60*60*24

      var hoursDifference = Math.floor(difference/1000/60/60);
      difference -= hoursDifference*1000*60*60

      var minutesDifference = Math.floor(difference/1000/60);
      difference -= minutesDifference*1000*60

      var secondsDifference = Math.floor(difference/1000);

      console.log('difference = ' +
        daysDifference + ' day/s ' +
        hoursDifference + ' hour/s ' +
        minutesDifference + ' minute/s ' +
        secondsDifference + ' second/s ');
  }

  timeDifference(start, end);
Stender
  • 2,446
  • 1
  • 14
  • 22
  • Is there a native way in JS to convert a date (such as "August 08, 20 00:20:18 GMT+00:00" into unix? I'm unable to format the `{{ event_date_and_time }}` into something similar to the one you've got in the `end_date` var. – Freddy Aug 05 '20 at 13:27
  • `event_date_and_time` is a unix timestamp. So if I do {{ event_date_and_time }} (`console.log` equivalent), it prints the following result: `1596888000000` (which I'm assuming is 8th August 2020 12:00pm) – Freddy Aug 05 '20 at 13:39
  • If you already has it as a timestamp, then there is no need for convertion - just `var end = {{event_date_and_time}}` and forget about the `var event_date ` that should be fine. – Stender Aug 05 '20 at 13:44
  • Understood, I now have just the following: `var end = {{ event_date_and_time }};`. However, when checking the console, I'm still seeing the log: `difference = -3 day/s 1 hour/s 47 minute/s 4 second/s ` - The difference from today to 8th August 12:00 is not that, it should be roughly `2 days, 21 hours, 13 minutes and 9 seconds`? Also, not sure why it's stating `-3 days`? – Freddy Aug 05 '20 at 13:49
  • I guess you need to reverse `var difference = date1 - date2;` so it becomes `var difference = date2 - date1;` - That would give me `difference = 2 day/s 10 hour/s 12 minute/s 24 second/s ` and then it is probably due to timezones – Stender Aug 05 '20 at 14:07