-3

I'm using FlipClock to do a countdown to a certain date. The date I'm counting down to is in this format: 0000-00-00 00:00:00.

I want to get how many seconds it is until the date. So I can use in my flipclock.

var clock = $('#countdown').FlipClock(seconds, {
    clockFace: 'HourlyCounter',
    countdown: true,
    callbacks: {
      stop: function() {
          //callback
      }
    }
});
Felipe Miosso
  • 7,309
  • 6
  • 44
  • 55
  • possible duplicate of [how to countdown to a date](http://stackoverflow.com/questions/9335140/how-to-countdown-to-a-date) – Tiago Marinho Jun 24 '14 at 13:06

1 Answers1

0

I guess your date is in an ISO 8601 like format of yyyy-mm-dd hh:mm:ss and should be interpreted as a local time. In that case, parse the string, convert it to a Date object and subtract it from now to get the difference in milliseconds. Divide by 1,000 to get the difference in seconds, e.g.

// Parse string in yyyy-mm-dd hh:mm:ss format
// Assume is a valid date and time in the system timezone
function parseString(s) {
  var b = s.split(/\D+/);
  return new Date(b[0], --b[1], b[2], b[3], b[4], b[5]);
}

// Get the difference between now and a provided date string
// in a format accepted by parseString
// If d is an invalid string, return NaN
// If the provided date is in the future, the returned value is +ve
function getTimeUntil(s) {
  var d = parseString(s);
  return d? (d - Date.now())/1000|0 : NaN;
}
RobG
  • 142,382
  • 31
  • 172
  • 209