Changeset 7537 in josm
- Timestamp:
- 2014-09-14T23:35:46+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/data/validator/opening_hours.js
r7514 r7537 4197 4197 4198 4198 selectors.week.push(function(week_from, week_to, is_range, period) { return function(date) { 4199 var ourweek = date.getWeekNumber();4199 var ourweek = getWeekNumber(date); 4200 4200 4201 4201 // console.log("week_from: %s, week_to: %s", week_from, week_to); … … 4242 4242 4243 4243 // http://stackoverflow.com/a/6117889 4244 Date.prototype.getWeekNumber = function(){ 4245 var d = new Date(+this); 4246 d.setHours(0,0,0); 4247 d.setDate(d.getDate()+4-(d.getDay()||7)); 4248 return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7); 4249 }; 4244 /* For a given date, get the ISO week number 4245 * 4246 * Based on information at: 4247 * 4248 * http://www.merlyn.demon.co.uk/weekcalc.htm#WNR 4249 * 4250 * Algorithm is to find nearest thursday, it's year 4251 * is the year of the week number. Then get weeks 4252 * between that date and the first day of that year. 4253 * 4254 * Note that dates in one year can be weeks of previous 4255 * or next year, overlap is up to 3 days. 4256 * 4257 * e.g. 2014/12/29 is Monday in week 1 of 2015 4258 * 2012/1/1 is Sunday in week 52 of 2011 4259 */ 4260 function getWeekNumber(d) { 4261 // Copy date so don't modify original 4262 d = new Date(+d); 4263 d.setHours(0,0,0); 4264 // Set to nearest Thursday: current date + 4 - current day number 4265 // Make Sunday's day number 7 4266 d.setDate(d.getDate() + 4 - (d.getDay()||7)); 4267 // Get first day of year 4268 var yearStart = new Date(d.getFullYear(),0,1); 4269 // Calculate full weeks to nearest Thursday 4270 return Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7) 4271 } 4250 4272 // http://stackoverflow.com/a/16591175 4251 4273 function getDateOfISOWeek(w, y) {
Note:
See TracChangeset
for help on using the changeset viewer.