javascript倒计时代码

其实就是用两个时间戳相减,余数转换为日期,就是所剩的年月日时分秒,不过年份-1970

$scope.timerID = null;

$scope.timerRunning = false;

$scope.showtime = function() {

if ($scope.data === undefined || $scope.data.auction_time_end === undefined) {

return;

}

Today = new Date();

var diffs = $scope.data.auction_time_end - Math.round(new Date().getTime()/1000);

if (diffs < 0) {

$("#spanLeft").html('0年, 0月, 0天, 0小时, 0分, 0秒');

return;

}

var newDate = new Date(diffs * 1000);

var year = newDate.getFullYear() - 1970;

var month = newDate.getMonth();

var date = newDate.getDate();

var hour = newDate.getHours();

var minutes = newDate.getMinutes();

var seconds = newDate.getSeconds();

s=year+'年, '+month+'月, '+date+'天, '+hour+'小时, '+minutes+'分, '+seconds+'秒';

$("#spanLeft").html(s);

$scope.timerID = setTimeout($scope.showtime, 1000);

$scope.timerRunning = true;

}

$scope.timerID = null;

$scope.timerRunning = false;

$scope.stopclock = function() {

if($scope.timerRunning)

clearTimeout($scope.timerID);

$scope.timerRunning = false;

}

$scope.startclock = function() {

$scope.stopclock();

$scope.showtime();

}

$scope.startclock()