Предлагаю полезные функции для работы с датами, которые как оказалось, мы очень часто используем.
Получение количества дней в месяце определенного года (решение в одну строку :))))))
var n = (m != 2 ?((m % 2) ^ (m > 7)) + 30 : (!(y % 400) || !(y % 4) && (y % 25) ? 29 : 28));
где m - месяц, y - год
где m - месяц, y - год
Получение номера недели (U.S. Standart)
function y2k(number) {
return (number 1000) ? number + 1900 : number;
}
function getWeek(day,month,year) {
year = y2k(year);
var when = new Date(year,month,day);
var newYear = new Date(year,0,1);
var offset = 7 + 1 - newYear.getDay();
if (offset == 8) offset = 1;
var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) - Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
var weeknum = Math.floor((daynum-offset+7)/7);
if (weeknum == 0) {
year--;
var prevNewYear = new Date(year,0,1);
var prevOffset = 7 + 1 - prevNewYear.getDay();
if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
}
return weeknum;
}
return (number 1000) ? number + 1900 : number;
}
function getWeek(day,month,year) {
year = y2k(year);
var when = new Date(year,month,day);
var newYear = new Date(year,0,1);
var offset = 7 + 1 - newYear.getDay();
if (offset == 8) offset = 1;
var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) - Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
var weeknum = Math.floor((daynum-offset+7)/7);
if (weeknum == 0) {
year--;
var prevNewYear = new Date(year,0,1);
var prevOffset = 7 + 1 - prevNewYear.getDay();
if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
}
return weeknum;
}
Получение номера недели (Europe and ISO Standard):
function y2k(number) {
return (number 1000) ? number + 1900 : number;
}
function getWeekEUR(day,month,year) {
year = y2k(year);
var when = new Date(year,month,day);
var newYear = new Date(year,0,1);
var modDay = newYear.getDay();
if (modDay == 0) modDay=6; else modDay--;
var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) -
Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
if (modDay 4 ) {
var weeknum = Math.floor((daynum+modDay-1)/7)+1;
}
else {
var weeknum = Math.floor((daynum+modDay-1)/7);
if (weeknum == 0) {
year--;
var prevNewYear = new Date(year,0,1);
var prevmodDay = prevNewYear.getDay();
if (prevmodDay == 0) prevmodDay = 6; else prevmodDay--;
if (prevmodDay 4) weeknum = 53; else weeknum = 52;
}
}
return + weeknum;
}
return (number 1000) ? number + 1900 : number;
}
function getWeekEUR(day,month,year) {
year = y2k(year);
var when = new Date(year,month,day);
var newYear = new Date(year,0,1);
var modDay = newYear.getDay();
if (modDay == 0) modDay=6; else modDay--;
var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) -
Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
if (modDay 4 ) {
var weeknum = Math.floor((daynum+modDay-1)/7)+1;
}
else {
var weeknum = Math.floor((daynum+modDay-1)/7);
if (weeknum == 0) {
year--;
var prevNewYear = new Date(year,0,1);
var prevmodDay = prevNewYear.getDay();
if (prevmodDay == 0) prevmodDay = 6; else prevmodDay--;
if (prevmodDay 4) weeknum = 53; else weeknum = 52;
}
}
return + weeknum;
}
Показать все комментарии
Войдите или зарегистрируйтесь, что бы комментировать