Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit 4d53baa

Browse files
authored
Merge pull request #198 from davidsandoz/first-day-of-week
Add option to change first day of week
2 parents a7de3d8 + f9d98ed commit 4d53baa

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ date-max-limit="" | String | false | Set a maximum date limit - you can use all
8787
date-set-hidden="" | String(Boolean) | false | Set the default date to be shown only in calendar and not in the input field
8888
date-disabled-dates="" | String([Date(), Date(), ...]) | false | Disable specific dates using an _Array_ of dates
8989
date-refocus="" | String(Boolean) | false | Set the datepicker to re-focus the input after selecting a date
90-
date-typer="" | String(Boolean) | false | Set the datepicker to update calendar date when user is typing a date, see validation [tips](#date-validation)
90+
date-typer="" | String(Boolean) | false | Set the datepicker to update calendar date when user is typing a date, see validation [tips](#date-validation)
91+
date-week-start-day="" | String(Number) | 0 | Set the first day of the week. Must be an integer between 0 (Sunday) and 6 (Saturday). (e.g. 1 for Monday)
9192
datepicker-class="" | String('class1 class2 class3') | false | Set custom class/es for the datepicker calendar
9293
datepicker-append-to="" | String('#id','.classname', 'body') | false | Append the datepicker to #id or .class element or to body
9394
datepicker-toggle="" | String(Boolean) | true | Set the datepicker to toggle its visibility on focus and blur

src/js/angular-datepicker.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
, pageDatepickers
160160
, hours24h = 86400000
161161
, htmlTemplate = generateHtmlTemplate(prevButton, nextButton)
162+
, n
162163
, onClickOnWindow = function onClickOnWindow() {
163164

164165
if (!isMouseOn &&
@@ -292,23 +293,32 @@
292293
, nextMonthDays = []
293294
, howManyNextDays
294295
, howManyPreviousDays
295-
, monthAlias;
296+
, monthAlias
297+
, dateWeekEndDay;
296298

297299
$scope.days = [];
300+
$scope.dateWeekStartDay = $scope.validateWeekDay($scope.dateWeekStartDay);
301+
dateWeekEndDay = ($scope.dateWeekStartDay + 6) % 7;
298302

299303
for (i = 1; i <= limitDate; i += 1) {
300304

301305
$scope.days.push(i);
302306
}
303307

304-
//get previous month days is first day in month is not Sunday
305-
if (firstDayMonthNumber === 0) {
308+
//get previous month days if first day in month is not first day in week
309+
if (firstDayMonthNumber === $scope.dateWeekStartDay) {
306310

307311
//no need for it
308312
$scope.prevMonthDays = [];
309313
} else {
310314

311-
howManyPreviousDays = firstDayMonthNumber;
315+
howManyPreviousDays = firstDayMonthNumber - $scope.dateWeekStartDay;
316+
317+
if (firstDayMonthNumber < $scope.dateWeekStartDay) {
318+
319+
howManyPreviousDays += 7;
320+
}
321+
312322
//get previous month
313323
if (Number(month) === 1) {
314324

@@ -326,10 +336,17 @@
326336
$scope.prevMonthDays = prevMonthDays.slice(-howManyPreviousDays);
327337
}
328338

329-
//get next month days is first day in month is not Sunday
330-
if (lastDayMonthNumber < 6) {
339+
//get next month days if last day in month is not last day in week
340+
if (lastDayMonthNumber === dateWeekEndDay) {
341+
//no need for it
342+
$scope.nextMonthDays = [];
343+
} else {
344+
howManyNextDays = 6 - lastDayMonthNumber + $scope.dateWeekStartDay;
345+
346+
if (lastDayMonthNumber < $scope.dateWeekStartDay) {
331347

332-
howManyNextDays = 6 - lastDayMonthNumber;
348+
howManyNextDays -= 7;
349+
}
333350
//get previous month
334351

335352
//return next month days
@@ -339,9 +356,6 @@
339356
}
340357
//attach previous month days
341358
$scope.nextMonthDays = nextMonthDays;
342-
} else {
343-
//no need for it
344-
$scope.nextMonthDays = [];
345359
}
346360
}
347361
, unregisterDataSetWatcher = $scope.$watch('dateSet', function dateSetWatcher(newValue) {
@@ -683,6 +697,16 @@
683697
return true;
684698
};
685699

700+
$scope.validateWeekDay = function isValidWeekDay(weekDay) {
701+
var validWeekDay = Number(weekDay, 10);
702+
// making sure that the given option is valid
703+
if (!validWeekDay || validWeekDay < 0 || validWeekDay > 6) {
704+
705+
validWeekDay = 0;
706+
}
707+
return validWeekDay;
708+
};
709+
686710
// respect previously configured interpolation symbols.
687711
htmlTemplate = htmlTemplate.replace(/{{/g, $interpolate.startSymbol()).replace(/}}/g, $interpolate.endSymbol());
688712
$scope.dateMonthTitle = $scope.dateMonthTitle || 'Select month';
@@ -692,6 +716,7 @@
692716
$scope.month = $filter('date')(date, 'MMMM');//december-November like
693717
$scope.monthNumber = Number($filter('date')(date, 'MM')); // 01-12 like
694718
$scope.day = Number($filter('date')(date, 'dd')); //01-31 like
719+
$scope.dateWeekStartDay = $scope.validateWeekDay($scope.dateWeekStartDay);
695720

696721
if ($scope.dateMaxLimit) {
697722

@@ -701,7 +726,13 @@
701726
$scope.year = Number($filter('date')(date, 'yyyy'));//2014 like
702727
}
703728
$scope.months = datetime.MONTH;
704-
$scope.daysInString = ['0', '1', '2', '3', '4', '5', '6'].map(function mappingFunc(el) {
729+
730+
$scope.daysInString = [];
731+
for (n = $scope.dateWeekStartDay; n <= $scope.dateWeekStartDay + 6; n += 1) {
732+
733+
$scope.daysInString.push(n % 7);
734+
}
735+
$scope.daysInString = $scope.daysInString.map(function mappingFunc(el) {
705736

706737
return $filter('date')(new Date(new Date('06/08/2014').valueOf() + A_DAY_IN_MILLISECONDS * el), 'EEE');
707738
});
@@ -819,6 +850,7 @@
819850
'dateDisabledDates': '@',
820851
'dateSetHidden': '@',
821852
'dateTyper': '@',
853+
'dateWeekStartDay': '@',
822854
'datepickerAppendTo': '@',
823855
'datepickerToggle': '@',
824856
'datepickerClass': '@',

0 commit comments

Comments
 (0)