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

Commit

Permalink
Allow to define the timezone
Browse files Browse the repository at this point in the history
Allow to define the timezone
[Time: 1h]
  • Loading branch information
jfmcode committed Mar 21, 2016
1 parent aea375c commit 604dd34
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngComboDatePicker",
"version": "1.1.2",
"version": "1.2.0",
"authors": [
"jfmdev <[email protected]>"
],
Expand Down
11 changes: 11 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ <h2>Change years order</h2>
<p>Choose a date: <ng-combo-date-picker ng-model="yearsOrder" ng-year-order="desc"></ng-combo-date-picker></p>
<p>Current selection is: <strong>{{ yearsOrder }}</strong>
</div>


<h2>Timezones</h2>
<div>
<p>Choose a date: <ng-combo-date-picker ng-model="timezone1" ng-date="2000-01-01" ng-timezone="-20"></ng-combo-date-picker></p>
<p>Current selection is: <strong>{{ timezone1 }}</strong>
<p>Choose a date: <ng-combo-date-picker ng-model="timezone2" ng-date="2000-01-01" ng-timezone="20"></ng-combo-date-picker></p>
<p>Current selection is: <strong>{{ timezone2 }}</strong>
<p>Choose a date: <ng-combo-date-picker ng-model="timezone3" ng-date="2000-01-01" ng-timezone="10.5"></ng-combo-date-picker></p>
<p>Current selection is: <strong>{{ timezone3 }}</strong>
</div>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-combo-date-picker",
"version": "1.1.2",
"version": "1.2.0",
"description": "An Angular directive to select dates using combo boxes",
"main": "source/ngComboDatePicker.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ Name | Description
`ngAttrsDate` | A JSON object with the attributes to add to the `select` element for the date.
`ngAttrsMonth` | A JSON object with the attributes to add to the `select` element for the month.
`ngAttrsYear` | A JSON object with the attributes to add to the `select` element for the year.
`ngYearOrder` | A _string_ indicating of the years must be sorted in "ascending" or "descending" order.
`ngYearOrder` | A _string_ indicating if the years must be sorted in "ascending" or "descending" order.
`ngTimezone` | A _number_ indicating timezone to be used. By default the timezone of the client is used.

License
-------
Expand Down
30 changes: 20 additions & 10 deletions source/ngComboDatePicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* ngComboDatePicker v1.1.2
* ngComboDatePicker v1.2.0
* http://github.com/jfmdev/ngComboDatePicker
* «Copyright 2015 Jose F. Maldonado»
* License: LGPLv3 (http://www.gnu.org/licenses/lgpl-3.0.html)
Expand All @@ -22,24 +22,35 @@ angular.module("ngComboDatePicker", [])
ngAttrsDate: '@',
ngAttrsMonth: '@',
ngAttrsYear: '@',
ngYearOrder: '@'
ngYearOrder: '@',
ngTimezone: '@'
},
controller: ['$scope', function($scope) {
// Define function for parse dates.
function parseDate(myDate) {
function parseDate(myDate, myTimezone) {
var res = null;
if(myDate !== undefined && myDate !== null) {
if(myDate instanceof Date) {
res = myDate;
} else {
if(typeof myDate == 'number' || typeof myDate == 'string') {
// Parse date.
res = new Date(isNaN(myDate)? myDate : parseInt(myDate, 10));

// Adjust timezone.
res = adjustTimezone(res, myTimezone);
}
}
}
return res;
};

// Define function for adjust timezone.
function adjustTimezone(myDate, myTimezone) {
var offset = isNaN(myTimezone)? new Date().getTimezoneOffset() : parseFloat(myTimezone)*60;
return new Date(myDate.getTime() + offset*60*1000);
}

// Define fuction for getting the maximum date for a month.
function maxDate(month, year) {
var res = 31;
Expand All @@ -65,15 +76,15 @@ angular.module("ngComboDatePicker", [])
}

// Initialize model.
$scope.ngModel = parseDate($scope.ngModel);
$scope.ngModel = parseDate($scope.ngModel, $scope.ngTimezone);

// Initialize attributes variables.
$scope.ngAttrsDate = parseJsonPlus($scope.ngAttrsDate);
$scope.ngAttrsMonth = parseJsonPlus($scope.ngAttrsMonth);
$scope.ngAttrsYear = parseJsonPlus($scope.ngAttrsYear);

// Verify if initial date was defined.
var initDate = parseDate($scope.ngDate);
var initDate = parseDate($scope.ngDate, $scope.ngTimezone);
if(initDate != null) $scope.ngModel = initDate;

// Initialize order.
Expand All @@ -84,15 +95,15 @@ angular.module("ngComboDatePicker", [])
}

// Initialize minimal and maximum values.
$scope.minDate = parseDate($scope.ngMinDate);
$scope.minDate = parseDate($scope.ngMinDate, $scope.ngTimezone);
if($scope.ngMinDate == null) {
var now = new Date();
var now = adjustTimezone(new Date(), $scope.ngTimezone);
$scope.minDate = new Date(now.getFullYear()-100, now.getMonth(), now.getDate(),
now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
}
$scope.maxDate = parseDate($scope.ngMaxDate);
$scope.maxDate = parseDate($scope.ngMaxDate, $scope.ngTimezone);
if($scope.maxDate == null) {
$scope.maxDate = new Date();
$scope.maxDate = adjustTimezone(new Date(), $scope.ngTimezone);
}

// Verify if selected date is in the valid range.
Expand Down Expand Up @@ -156,7 +167,6 @@ angular.module("ngComboDatePicker", [])
}
};


// When the model is updated, update the combo boxes.
$scope.modelUpdated = function() {
// Update combo boxes.
Expand Down
4 changes: 2 additions & 2 deletions source/ngComboDatePicker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 604dd34

Please sign in to comment.