Last active
November 2, 2015 11:06
-
-
Save dnikonov/d1c46474a4bc01605e15 to your computer and use it in GitHub Desktop.
JAVASCRIPT: days left
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var leftDaysHoursMinutes = function(){ | |
var today = new Date(); | |
var nextEVENT = new Date(2015, 10, 11, 0, 0, 0, 0); // месяц начинается с нуля | |
var msDelta = nextEVENT.getTime() - today.getTime(); | |
if (msDelta > 0) { | |
// дней | |
var msPerDay = 24 * 60 * 60 * 1000; | |
var daysLeft = Math.floor(msDelta / msPerDay); | |
var sDaysLeft = String(daysLeft); | |
var sDaysText = "дней"; | |
var daysLeftLength = sDaysLeft.length; | |
if (sDaysLeft.charAt(daysLeftLength - 2) != "1"){ | |
if (sDaysLeft.charAt(daysLeftLength - 1) == "2" || sDaysLeft.charAt(daysLeftLength - 1) == "3" || sDaysLeft.charAt(daysLeftLength - 1) == "4"){ | |
sDaysText = "дня"; | |
} else if (sDaysLeft.charAt(daysLeftLength - 1) == "1"){ | |
sDaysText = "день"; | |
} | |
} | |
// часов | |
var msPerHour = 60 * 60 * 1000; | |
var hoursLeft = Math.floor((msDelta - (msPerDay * daysLeft)) / msPerHour); | |
var sHoursLeft = String(hoursLeft); | |
var sHoursText = "часов"; | |
var daysLeftLength = sHoursLeft.length; | |
if (sHoursLeft.charAt(daysLeftLength - 2) != "1"){ | |
if (sHoursLeft.charAt(daysLeftLength - 1) == "2" || sHoursLeft.charAt(daysLeftLength - 1) == "3" || sHoursLeft.charAt(daysLeftLength - 1) == "4"){ | |
sHoursText = "часа"; | |
} else if (sHoursLeft.charAt(daysLeftLength - 1) == "1"){ | |
sHoursText = "час"; | |
} | |
} | |
// минут | |
var msPerMinute = 60 * 1000; | |
var minutesLeft = Math.floor((msDelta - (msPerDay * daysLeft + msPerHour * hoursLeft)) / msPerMinute); | |
var sMinutesLeft = String(minutesLeft); | |
var sMinutesText = "минут"; | |
var daysLeftLength = sMinutesLeft.length; | |
if (sMinutesLeft.charAt(daysLeftLength - 2) != "1"){ | |
if (sMinutesLeft.charAt(daysLeftLength - 1) == "2" || sMinutesLeft.charAt(daysLeftLength - 1) == "3" || sMinutesLeft.charAt(daysLeftLength - 1) == "4"){ | |
sMinutesText = "минуты"; | |
} else if (sMinutesLeft.charAt(daysLeftLength - 1) == "1"){ | |
sMinutesText = "минута"; | |
} | |
} | |
// секунд | |
var msPerSecond = 1000; | |
var secondsLeft = Math.floor((msDelta - (msPerDay * daysLeft + msPerHour * hoursLeft + msPerMinute * minutesLeft)) / msPerSecond); | |
var sSecondsLeft = String(secondsLeft); | |
var sSecondsText = "секунд"; | |
var daysLeftLength = sSecondsLeft.length; | |
if (sSecondsLeft.charAt(daysLeftLength - 2) != "1"){ | |
if (sSecondsLeft.charAt(daysLeftLength - 1) == "2" || sSecondsLeft.charAt(daysLeftLength - 1) == "3" || sSecondsLeft.charAt(daysLeftLength - 1) == "4"){ | |
sSecondsText = "секунды"; | |
} else if (sSecondsLeft.charAt(daysLeftLength - 1) == "1"){ | |
sSecondsText = "секунда"; | |
} | |
} | |
} else { | |
var daysLeft = 0; | |
var sDaysText = "дней"; | |
var hoursLeft = 0; | |
var sHoursText = "часов"; | |
var minutesLeft = 0; | |
var sMinutesText = "минут"; | |
var secondsLeft = 0; | |
var sSecondsText = "секунд"; | |
}; | |
$('.timer .d').html(daysLeft + "<span>" + sDaysText + "</span>"); | |
$('.timer .h').html(hoursLeft + "<span>" + sHoursText + "</span>"); | |
$('.timer .m').html(minutesLeft + "<span>" + sMinutesText + "</span>"); | |
$('.timer .s').html(secondsLeft + "<span>" + sSecondsText + "</span>"); | |
}; | |
leftDaysHoursMinutes(); | |
setInterval(leftDaysHoursMinutes, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment