2

I am trying to make a countdown clock until the inauguration on January 20, 2017 at approximately 12:00pm using flipclock.js

How do I calculate the difference (in seconds) between the current time and that date/time?

I currently have:

var inauguration = new Date("January 20, 2017 12:00:00");
var now = Date.now();
var diff = inauguration - now;
var clock = $('#clock').FlipClock(diff, {
    countdown: true,
    clockFace: "DailyCounter"
});

Got it working:

    var inauguration = new Date(2017, 00, 20, 12, 0, 0, 0);
    var now = Date.now();
    var diff = inauguration.getTime() - now;
    diff = diff / 1000;
    var clock = $('#clock').FlipClock(diff, {
        countdown: true,
        clockFace: "DailyCounter"
    });
  • @Dalorzo Well, no. This is something different. – Praveen Kumar Purushothaman Jan 02 '17 at 00:21
  • 1
    Haha yeah, although I think it is a bit different than what @Dalorzo linked. We got it working with the answer below. Teamwork makes the dream work `:D` – Parker Thomas Jan 02 '17 at 00:34
  • When creating a date, don't parse a string, pass arguments directly to the Date constructor: `new Date(2017,0,20,12)`. It's less to type and less problematic given that built–in parsing is mostly implementation dependent. ;-) – RobG Jan 02 '17 at 05:55

1 Answers1

3

Currently inauguration is a Date object. You can get the number of seconds by .getTime() function. You need to change:

var diff = inauguration.getTime() - now;
//---------------------^^^^^^^^^^

Your working code should be:

var inauguration = new Date(2017, 00, 20, 12, 0, 0, 0);
var now = Date.now();
var diff = inauguration.getTime() - now;
diff = diff / 1000;
var clock = $('#clock').FlipClock(diff, {
    countdown: true,
    clockFace: "DailyCounter"
});

The idea of getTime() was part of the solution, and along with a couple of other minor changes the above code is now working.

Also, the original implementation works as well:

var inauguration = new Date("January 20, 2017 12:00:00");
var now = Date.now();
var diff = (inauguration.getTime() - now) / 1000;
var clock = $('#clock').FlipClock(diff, {
  countdown: true,
  clockFace: "DailyCounter"
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.8/flipclock.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.8/flipclock.min.js"></script>
<div id="clock"></div>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252