|
const test_start = 1986; |
|
let test_years = {}; |
|
let test_days = []; |
|
|
|
function weeksInYear (year) { |
|
return Math.max(moment('12-31-'+ year).isoWeek(), moment('12-24-'+ year).isoWeek()); |
|
} |
|
|
|
function compareWeek (date) { |
|
return { |
|
format: moment(date).format('llll'), |
|
iso: moment(date).format('GGGG WW E'), |
|
locale: moment(date).format('gggg ww e'), |
|
}; |
|
} |
|
|
|
_.range(60).forEach(function (y) { |
|
const year = test_start + y; |
|
const locale = moment(year +'-01-01').weeksInYear() === 53; |
|
const iso = moment(year +'-01-01').isoWeeksInYear() === 53; |
|
const which = iso ? 'iso' : 'locale'; |
|
if (locale && iso) { alert(year); } |
|
test_years[year] = locale || iso ? 53 +' ('+ which +')' : 52; |
|
|
|
test_days.push(compareWeek(year +'001')); |
|
test_days.push(compareWeek(year +'003')); |
|
test_days.push(compareWeek(year +'363')); |
|
test_days.push(compareWeek(year +'365')); |
|
|
|
console.log(getFirstDayOfYear(year)); |
|
}); |
|
|
|
// console.log('Years', test_years); |
|
// console.log('Days', test_days); |
|
|
|
// Decision: use ISO because it makes math easier |
|
//en.wikipedia.org/wiki/ISO_week_date#Advantages |
|
|
|
let test_current = moment().year(); |
|
|
|
function render (year) { |
|
const numDays = moment([year]).isLeapYear() ? 366 : 365; |
|
|
|
let days = []; |
|
|
|
_.range(numDays).forEach(function (n) { |
|
const ordinal = ('00'+ (n + 1)).slice(-3); |
|
days.push(moment(year + ordinal).format('GGGGWWE')); |
|
}); |
|
|
|
// console.log(year, days); |
|
} |
|
|
|
render(test_current); |
|
|
|
document.onkeydown = function (e) { |
|
// left |
|
if (e.which === 37) { |
|
test_current--; |
|
render(test_current); |
|
} |
|
// right |
|
if (e.which === 39) { |
|
test_current++; |
|
render(test_current); |
|
} |
|
}; |
|
|
|
// try to find the first day of the iso week year |
|
function manualLogs () { |
|
// console.log(moment('2011-01-01').format('GGGGWWE')); |
|
console.log(moment('2011-01-03').format('GGGGWWE')); |
|
|
|
// console.log(moment('2012-01-01').format('GGGGWWE')); |
|
console.log(moment('2012-01-02').format('GGGGWWE')); |
|
|
|
console.log(moment('2012-12-31').format('GGGGWWE')); |
|
// console.log(moment('2013-01-01').format('GGGGWWE')); |
|
|
|
console.log(moment('2013-12-30').format('GGGGWWE')); |
|
// console.log(moment('2014-01-01').format('GGGGWWE')); |
|
|
|
console.log(moment('2014-12-29').format('GGGGWWE')); |
|
// console.log(moment('2015-01-01').format('GGGGWWE')); |
|
|
|
// console.log(moment('2016-01-01').format('GGGGWWE')); |
|
console.log(moment('2016-01-04').format('GGGGWWE')); |
|
|
|
// console.log(moment('2017-01-01').format('GGGGWWE')); |
|
console.log(moment('2017-01-02').format('GGGGWWE')); |
|
|
|
console.log(moment('2018-01-01').format('GGGGWWE')); |
|
|
|
console.log(moment('2018-12-31').format('GGGGWWE')); |
|
// console.log(moment('2019-01-01').format('GGGGWWE')); |
|
|
|
console.log(moment('2019-12-30').format('GGGGWWE')); |
|
// console.log(moment('2020-01-01').format('GGGGWWE')); |
|
} |
|
// manualLogs(); |
|
|
|
function getFirstDayOfYear (year) { |
|
// const start = moment(year +'-01-01').subtract(3, 'd'); |
|
// _.range(7).forEach(function (i) { |
|
// const day = start.add(i, 'd').format('GGGGWWE'); |
|
// if (day.slice(-3) === '011') { |
|
// console.log(day); |
|
// } |
|
// }); |
|
return moment(year +'W011').format('llll'); |
|
} |
|
|
|
// various utility functions |
|
function getWeeksArr (year) { |
|
const numWeeks = moment([year]).isoWeeksInYear(); |
|
|
|
let weeks = []; |
|
|
|
_.range(numWeeks).forEach(function (w) { |
|
const week = ('0'+ (w + 1)).slice(-2); |
|
weeks.push(_.range(7).map(function (d) { |
|
const day = year +'W'+ week + (d + 1); |
|
return { |
|
locale: moment(day).format('llll'), |
|
iso: day, |
|
}; |
|
})); |
|
}); |
|
|
|
return weeks; |
|
} |
|
|
|
function getWeeksObj (year) { |
|
const init = Date.now(); |
|
const numWeeks = moment([year]).isoWeeksInYear(); |
|
|
|
let weeks = {}; |
|
|
|
_.range(numWeeks).forEach(function (w) { |
|
const week = ('0'+ (w + 1)).slice(-2); |
|
let days = {}; |
|
_.range(7).forEach(function (d) { |
|
const day = year +'W'+ week + (d + 1); |
|
days[day] = { |
|
unix: moment(day).format('X'), |
|
}; |
|
}); |
|
weeks[week] = days; |
|
}); |
|
|
|
console.log('getWeeksObj took '+ (Date.now() - init) +'ms'); |
|
return weeks; |
|
// problem: when a for..in tries to loop through this, the padded numbers will be at the end e.g. …51, 52, 01, 02… :( |
|
} |
|
|
|
function getDaysArr (year) { |
|
const numWeeks = moment([year]).isoWeeksInYear(); |
|
const numDays = numWeeks === 53 ? 371 : 364; |
|
|
|
let days = []; |
|
|
|
_.range(numWeeks).forEach(function (w) { |
|
const week = ('0'+ (w + 1)).slice(-2); |
|
_.range(7).forEach(function (d) { |
|
const day = year +'W'+ week + (d + 1); |
|
days.push({ |
|
locale: moment(day).format('llll'), |
|
iso: day, |
|
}); |
|
}); |
|
}); |
|
|
|
return days; |
|
} |
|
|
|
function getDaysObj (year) { |
|
const numWeeks = moment([year]).isoWeeksInYear(); |
|
const numDays = numWeeks === 53 ? 371 : 364; |
|
|
|
let days = {}; |
|
|
|
_.range(numWeeks).forEach(function (w) { |
|
const week = ('0'+ (w + 1)).slice(-2); |
|
_.range(7).forEach(function (d) { |
|
const day = year +'W'+ week + (d + 1); |
|
days[day] = moment(day).format('llll'); |
|
}); |
|
}); |
|
|
|
return days; |
|
} |