Reputation: 15985
Is there any jQuery plugin available to select Week / Year for example 1 / 2012
or 53 / 2010
just like Date Picker?
I stumbled around but could not find matching plugin. I am thinking of simple selection box but seems picker would give good UX.
Upvotes: 1
Views: 2338
Reputation: 12579
I've done that for 1 of my projects restyling jquery UI datepicker. Also getting week number from date is not as simple as it seems: http://brainhog.blogspot.ie/2011/07/get-me-week.html
Anyways, here's the complete solution:
css:
.ui-datepicker .ui-datepicker-calendar { border-collapse: separate; border-spacing: 0 2px; }
.ui-datepicker .ui-datepicker-calendar td { padding: 0; border: 1px solid #D3D3D3; border-width: 1px 0; }
.ui-datepicker .ui-datepicker-calendar td:last-child { border-right-width: 1px; }
.ui-datepicker .ui-datepicker-calendar td a { border: none; }
.ui-datepicker .ui-datepicker-calendar tbody tr:hover td { border-color: #FCEFA1; }
.ui-datepicker-calendar tbody tr:hover td a { background: #FCFAF1; }
.ui-datepicker-calendar tbody tr:hover .ui-datepicker-week-col,
.ui-datepicker-calendar tbody tr .ui-datepicker-week-col { background: #757575; padding: 0.2em; color: #fff; text-align: right; border: 1px solid #3F3F3F; }
#weekPicker { font-size: .7em; }
js:
$('#weekPicker').datepicker({
firstDay: 1,
showWeek: true,
weekHeader: '',
dateFormat: 'yy-mm-dd',
beforeShow: function(input, inst){
var yw = input.value.split('-'), dat = getDateFromWeek(yw[0], yw[1]);
$('#weekPicker').datepicker("setDate", dat);
},
onClose: function(input, inst){
var yw = new Date(input).getFullWeek();
inst.input.val(yw.y+'-'+yw.w);
$('#weekPicker2').html('<b>Week ' + yw.w+'</b>: '+input);
}
});
and you also need these functions:
//Returns ISO 8601 week number and year
Date.prototype.getFullWeek = function(){
var jan1, w, d = new Date(this);
d.setDate(d.getDate()+4-(d.getDay()||7)); // Set to nearest Thursday: current date + 4 - current day number, make Sunday's day number 7
jan1 = new Date(d.getFullYear(),0,1); // Get first day of year
w = Math.ceil((((d-jan1)/86400000)+1)/7); // Calculate full weeks to nearest Thursday
return {y: d.getFullYear(), w: w };
};
//Returns ISO 8601 week number
Date.prototype.getWeek = function(){ return this.getFullWeek().w; };
var getWeeksInYear = function(y){ return new Date(y,11,28).getFullWeek().w; };
Let me know if you have any issues/questions and whether this works for you.
Upvotes: 3