- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
function timeToString(year, month, day, hour, minute, sec, msec) {
var date = new Date(year, month, day, hour, minute, sec, msec);
var now = new Date();
var msec = now.getTime() - date.getTime(); // Миллисекунды
var sec = Math.round(msec / 1000);
var years = Math.floor(sec / (86400 * 365));
var days = Math.floor((sec - years * 86400 * 365) / 86400);
var hours = Math.floor((sec - years * 86400 * 365 - days * 86400) / 3600);
var minutes = Math.floor((sec - years * 86400 * 365 - days * 86400 - hours * 3600) / 60);
var seconds = sec - years * 86400 * 365 - days * 86400 - hours * 3600 - minutes * 60;
// Проверяем, были ли високосные года?
var i;
for(i = date.getFullYear(); i <= now.getFullYear(); i++) {
if(i % 4 === 0) {
if(i === now.getFullYear()) {
if(now.getMonth > 2) {
days++;
}
elseif(now.getMonth === 2) {
if(now.getDay === 28) {
days++;
}
}
}
else
{
days++;
}
}
var yearName;
var yearOst = years % 10;
if(years === 1 || (yearOst === 1 && years > 20))
{
yearName = 'год'
}
else if((years === 2 || years === 3 || years === 4) || ((yearOst === 2 || yearOst === 3 || yearOst === 4) && years > 20))
{
yearName = 'года';
}
else
{
yearName = 'лет';
}
var hourName;
var hourOst = hours % 10;
if(hours === 1 || (hourOst === 1 && hours > 20))
{
hourName = 'час'
}
else if((hours === 2 || hours === 3 || hours === 4) || ((hourOst === 2 || hourOst === 3 || hourOst === 4) && hours > 20))
{
hourName = 'часа';
}
else
{
hourName = 'часов';
}
var dayName;
var dayOst = days % 10;
if(days === 1 || (dayOst === 1 && days > 20))
{
dayName = 'день'
}
else if((days === 2 || days === 3 || days === 4) || ((dayOst === 2 || dayOst === 3 || dayOst === 4) && days > 20))
{
dayName = 'дня';
}
else
{
dayName = 'дней';
}
var minuteName;
var minuteOst = minutes % 10;
if(minutes === 1 || (minuteOst === 1 && minutes > 20))
{
minuteName = 'минута'
}
else if((minutes === 2 || minutes === 3 || minutes === 4) || ((minuteOst === 2 || minuteOst === 3 || minuteOst === 4) && minutes > 20))
{
minuteName = 'минуты';
}
else
{
minuteName = 'минут';
}
var secondName;
var secondOst = seconds % 10;
if(seconds === 1 || (secondOst === 1 && seconds > 20))
{
secondName = 'секунда'
}
else if((seconds === 2 || seconds === 3 || seconds === 4) || ((secondOst === 2 || secondOst === 3 || secondOst === 4) && seconds > 20))
{
secondName = 'секунды';
}
else {
secondName = 'секунд';
}
document.getElementById("time").innerHTML = years + " " + yearName + ", " + days + " " + dayName + ", " + hours + " " + hourName + ", " + minutes + " " + minuteName + ", " + seconds + " " + secondName;
}
Функция для подсчета того, сколько времени прошло с указанной даты.
JavaScript начал изучать вчера. Чувствую, что уже можно заканчивать.
Пришлось код урезать, чтобы вместиться в 100 строк. Более-менее читаемый тут: http://dumpz.org/573381/