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

Commit a4a83d8

Browse files
committed
Adding option to select the first day of the week
1 parent a6a28ca commit a4a83d8

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-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: 27 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 &&
@@ -301,14 +302,14 @@
301302
$scope.days.push(i);
302303
}
303304

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

307308
//no need for it
308309
$scope.prevMonthDays = [];
309310
} else {
310311

311-
howManyPreviousDays = firstDayMonthNumber;
312+
howManyPreviousDays = firstDayMonthNumber - $scope.dateWeekStartDay;
312313
//get previous month
313314
if (Number(month) === 1) {
314315

@@ -326,10 +327,12 @@
326327
$scope.prevMonthDays = prevMonthDays.slice(-howManyPreviousDays);
327328
}
328329

329-
//get next month days is first day in month is not Sunday
330-
if (lastDayMonthNumber < 6) {
331-
332-
howManyNextDays = 6 - lastDayMonthNumber;
330+
//get next month days if last day in month is not last day in week
331+
if (lastDayMonthNumber === $scope.dateWeekEndDay) {
332+
//no need for it
333+
$scope.nextMonthDays = [];
334+
} else {
335+
howManyNextDays = 6 - lastDayMonthNumber + $scope.dateWeekStartDay;
333336
//get previous month
334337

335338
//return next month days
@@ -339,9 +342,6 @@
339342
}
340343
//attach previous month days
341344
$scope.nextMonthDays = nextMonthDays;
342-
} else {
343-
//no need for it
344-
$scope.nextMonthDays = [];
345345
}
346346
}
347347
, unregisterDataSetWatcher = $scope.$watch('dateSet', function dateSetWatcher(newValue) {
@@ -692,6 +692,11 @@
692692
$scope.month = $filter('date')(date, 'MMMM');//december-November like
693693
$scope.monthNumber = Number($filter('date')(date, 'MM')); // 01-12 like
694694
$scope.day = Number($filter('date')(date, 'dd')); //01-31 like
695+
$scope.dateWeekStartDay = parseInt($scope.dateWeekStartDay, 10);
696+
// making sure that the given option is valid
697+
if (!Number.isInteger($scope.dateWeekStartDay) || $scope.dateWeekStartDay < 0 || $scope.dateWeekStartDay > 6) {
698+
$scope.dateWeekStartDay = 0;
699+
}
695700

696701
if ($scope.dateMaxLimit) {
697702

@@ -701,11 +706,21 @@
701706
$scope.year = Number($filter('date')(date, 'yyyy'));//2014 like
702707
}
703708
$scope.months = datetime.MONTH;
704-
$scope.daysInString = ['0', '1', '2', '3', '4', '5', '6'].map(function mappingFunc(el) {
709+
710+
$scope.daysInString = ['0', '1', '2', '3', '4', '5', '6'];
711+
if ($scope.dateWeekStartDay > 0) {
712+
// shifting the first day of the week according to the given option
713+
for (n = 0; n < $scope.dateWeekStartDay; n += 1) {
714+
$scope.daysInString.push($scope.daysInString.shift());
715+
}
716+
}
717+
$scope.daysInString.map(function mappingFunc(el) {
705718

706719
return $filter('date')(new Date(new Date('06/08/2014').valueOf() + A_DAY_IN_MILLISECONDS * el), 'EEE');
707720
});
708721

722+
$scope.dateWeekEndDay = $scope.daysInString[7];
723+
709724
//create the calendar holder and append where needed
710725
if ($scope.datepickerAppendTo &&
711726
$scope.datepickerAppendTo.indexOf('.') !== -1) {
@@ -819,6 +834,7 @@
819834
'dateDisabledDates': '@',
820835
'dateSetHidden': '@',
821836
'dateTyper': '@',
837+
'dateWeekStartDay': '@',
822838
'datepickerAppendTo': '@',
823839
'datepickerToggle': '@',
824840
'datepickerClass': '@',

0 commit comments

Comments
 (0)