dmitrii
11/12/2017 - 7:04 PM

TimeCount + Склонения

$(function(){
	countIt();
});

function countIt(){
	year = 2017;
	month = 11;
	day = 27;
	hours = 00;
	minutes = 00;
	seconds = 00;

	setTimeout(function(){
		endDate = new Date(year, (month - 1), day, hours, minutes, seconds, 00);
		thisDate  = new Date();
		thisDate  = new Date(thisDate.getFullYear(), thisDate.getMonth(), thisDate.getDate(), thisDate.getHours(), thisDate.getMinutes(), thisDate.getSeconds(), 00, 00);

		var daysLeft = parseInt((endDate-thisDate)/86400000);
		var hoursLeft = parseInt((endDate-thisDate)/3600000); 
		var minutsLeft = parseInt((endDate-thisDate)/60000);
		var secondsLeft = parseInt((endDate-thisDate)/1000);

		seconds = minutsLeft*60;
		seconds = secondsLeft-seconds;

		minutes = hoursLeft*60;
		minutes = minutsLeft-minutes;

		hours = daysLeft*24;
		hours = (hoursLeft-hours) < 0 ? 0 : hoursLeft-hours;

		days = daysLeft;

		startCount(days, hours, minutes,seconds);
	}, 1000);
}

function declOfNum(number, titles){  
    var cases = [2, 0, 1, 1, 1, 2];  
    return titles[ (number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5] ];  
}

function startCount(days, hours, minutes, seconds){
	$('.counter').each(function(index, el) {
		var digits = $(this).children();
		$(digits[0]).find('.counter-digit').html(days);
		$(digits[1]).find('.counter-digit').html(hours);
		$(digits[2]).find('.counter-digit').html(minutes);
		$(digits[3]).find('.counter-digit').html(seconds);

		if ($('.counter-digit.rus').length > 0) {

		var d_name = declOfNum(days,['день','дня','дней']);
		var h_name = declOfNum(hours,['час','часа','часов']);
		var m_name = declOfNum(minutes,['минута','минуты','минут']);
		var s_name = declOfNum(seconds,['секунда','секунды','секунд']);

		$(digits[0]).find('.counter-digit.rus').next().html(d_name);
		$(digits[1]).find('.counter-digit.rus').next().html(h_name);
		$(digits[2]).find('.counter-digit.rus').next().html(m_name);
		$(digits[3]).find('.counter-digit.rus').next().html(s_name);

		}

	});
	countIt();
}