-3

I am using an API that returns the time like this deal_expiration_time:1469396377 which looks like its seconds from 1970. I am trying to get a countdown in MM/SS This is in angular so I apparently can't use jQuery? I have struggled for a while and am stumped with this code. I'm trying to make it so its input: 1469396377 | output: 08:21

function(){
  var x1, secs1 = 510;
  x1 = setInterval(function(){myFunc1(9)}, 1000);

  function myFunc1(timerId){
    var minutes = Math.floor(secs1 / 60);
    var seconds = secs1 % 60;
    $('#timer_'+timerId).html(minutes + ':' + seconds); //assuming there is a label with Id 'timer'

    secs1--;
    if(secs1 == 0){
      document.getElementById('timer_' + timerId).style.hidden = true;
      clearInterval(x1);
    }
  }
}
Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
K Spring
  • 5
  • 1
  • 2
    This is Unix time, or epoch time. You can use `Date.now()` or `+new Date` to get it as suggested on [this similar question](http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript). You may have to truncate the milliseconds. – Havenard Jul 25 '16 at 02:09
  • *"This is in angular so i apparently can't use jQuery??"* - I don't see why you can't use them together, but in any case jQuery isn't a date/time manipulation library so I don't know why you want to use it here... – nnnnnn Jul 25 '16 at 02:12
  • Is there more code you can provide? I don't see anything specific to Angular in your example. Either way, check out http://stackoverflow.com/questions/12050268/angularjs-make-a-simple-countdown – Ash Jul 25 '16 at 02:25

1 Answers1

0

You can get the current time using Date.now() and then manually calculate the difference in time. This function gets the difference between the given time and the current time, calculates the difference in seconds and in minutes. This difference is then formatted as a countdown string and then returned.

function getTimeDifference(expirationTime) {
  var now = Date.now();
  var diff = expirationTime - now;
  var secDiff = Math.round(diff/1000);
  var minDiff = Math.round(secDiff / 60);
  var diffStr = minDiff + ":" + (secDiff - minDiff * 60);
  return diffStr
}
adeora
  • 557
  • 1
  • 8
  • 21