jQuery DatePicker has the ability to return weeknumber based on a selected date.
How can I get Start and End Date of that weeknumber based on a selected date?
Currently I have following code, I but I would appreciate any tips to improve this for above requirement.
<script type="text/javascript">
$(document).ready(function () {
$('#' + '<%= weekTBox.ClientID %>').datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
constrainInput: true,
showWeek: true,
firstDay: 1,
showOtherMonths: true,
selectOtherMonths: true
,onSelect: function (dateText, ui) {
//desired functionality should be here...
$this.val($.datepicker.iso8601Week(new Date(dateText)));
}
});
});
</script>
jQuery DatePicker has the ability to return weeknumber based on a selected date.
How can I get Start and End Date of that weeknumber based on a selected date?
Currently I have following code, I but I would appreciate any tips to improve this for above requirement.
<script type="text/javascript">
$(document).ready(function () {
$('#' + '<%= weekTBox.ClientID %>').datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
constrainInput: true,
showWeek: true,
firstDay: 1,
showOtherMonths: true,
selectOtherMonths: true
,onSelect: function (dateText, ui) {
//desired functionality should be here...
$this.val($.datepicker.iso8601Week(new Date(dateText)));
}
});
});
</script>
Share
Improve this question
asked Jul 23, 2014 at 4:55
bonCodigobonCodigo
14.4k1 gold badge50 silver badges92 bronze badges
2 Answers
Reset to default 6Given a year and week number, the following returns a date that is the Monday at the start of the week (assuming you want weeks starting on Monday).
Week numbers are based on the week that has the first Thursday of the year, so this function looks at 1 January and determines the first day of week 1 (Monday), then adds days for the number of weeks * 7 to get the first Monday of the subject week. The end of the week is the following Sunday, so just add 7 to the date.
// Returns a Date object for Monday at the
// start of the specified week
function dateFromWeekNumber(year, week) {
var d = new Date(year, 0, 1);
var dayNum = d.getDay();
var diff = --week * 7;
// If 1 Jan is Friday to Sunday, go to next week
if (!dayNum || dayNum > 4) {
diff += 7;
}
// Add required number of days
d.setDate(d.getDate() - d.getDay() + ++diff);
return d;
}
console.log(dateFromWeekNumber(2014, 1)); // Mon 30 Dec 2013
console.log(dateFromWeekNumber(2016, 1)); // Mon 04 Jan 2016
console.log(dateFromWeekNumber(2014, 30)); // Mon 21 Jul 2014
You may want to add validation, e.g. that week is an integer greater than zero and less than 54.
Edit
Modified to return Monday of week number.
FIDDLE
$( "#datepicker" ).datepicker({
showWeek: true,
firstDay: 1,
onSelect: function(date){
var d = new Date(date);
var d1 = new Date(date);
var index = d.getDay();
// console.log(index)
if(index == 0) {
d.setDate(d.getDate() - 6);
d1.setDate(d1.getDate() - 2);
}
else if(index == 1) {
d.setDate(d.getDate());
d1.setDate(d1.getDate()+4);
}
else if(index != 1 && index > 0) {
d.setDate(d.getDate() - (index - 1));
d1.setDate(d1.getDate() + (index + 1));
//console.log(d.getDate() - (index - 1))
}
console.log('Week number::'+$.datepicker.iso8601Week(new Date(d)));
console.log("start date::"+d)
console.log("End date::"+d1)
//$(this).val(d);
}
});
});
I think This will help u..