Skip to content

Commit 07bbada

Browse files
Mehdi ChamoumaMehdi Chamouma
authored andcommitted
Add support for months and years
1 parent d34157e commit 07bbada

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

app/js/timer.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,38 +106,64 @@ angular.module('timer', [])
106106
function calculateTimeUnits() {
107107

108108
// compute time values based on maxTimeUnit specification
109-
if(!$scope.maxTimeUnit) {
109+
if(!$scope.maxTimeUnit || $scope.maxTimeUnit === 'day') {
110110
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
111111
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
112112
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
113-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
113+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
114+
$scope.months = 0;
115+
$scope.years = 0;
114116
} else if($scope.maxTimeUnit === 'second') {
115117
$scope.seconds = Math.floor($scope.millis / 1000);
116118
$scope.minutes = 0;
117119
$scope.hours = 0;
118120
$scope.days = 0;
121+
$scope.months = 0;
122+
$scope.years = 0;
119123
} else if($scope.maxTimeUnit === 'minute') {
120124
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
121125
$scope.minutes = Math.floor($scope.millis / 60000);
122126
$scope.hours = 0;
123127
$scope.days = 0;
128+
$scope.months = 0;
129+
$scope.years = 0;
124130
} else if($scope.maxTimeUnit === 'hour') {
125131
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
126132
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
127133
$scope.hours = Math.floor($scope.millis / 3600000);
128134
$scope.days = 0;
135+
$scope.months = 0;
136+
$scope.years = 0;
137+
} else if($scope.maxTimeUnit === 'month') {
138+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
139+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
140+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
141+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
142+
$scope.months = Math.floor((($scope.millis / (3600000)) / 24) / 30);
143+
$scope.years = 0;
144+
} else if($scope.maxTimeUnit === 'year') {
145+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
146+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
147+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
148+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
149+
$scope.months = Math.floor(($scope.millis / (3600000)) / 24 / 30);
150+
$scope.years = Math.floor(($scope.millis / (3600000)) / 24 / 30 / 12);
129151
}
130152

131153
// plural - singular unit decision
132154
$scope.secondsS = $scope.seconds==1 ? '' : 's';
133155
$scope.minutesS = $scope.minutes==1 ? '' : 's';
134156
$scope.hoursS = $scope.hours==1 ? '' : 's';
135157
$scope.daysS = $scope.days==1 ? '' : 's';
158+
$scope.monthsS = $scope.months==1 ? '' : 's';
159+
$scope.yearsS = $scope.years==1 ? '' : 's';
136160
//add leading zero if number is smaller than 10
137161
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
138162
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
139163
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
140164
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
165+
$scope.mmonths = $scope.months < 10 ? '0' + $scope.months : $scope.months;
166+
$scope.yyears = $scope.years < 10 ? '0' + $scope.years : $scope.years;
141167

142168
}
143169
//determine initial values of time units and add AddSeconds functionality

dist/angular-timer.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* angular-timer - v1.1.0 - 2014-04-23 5:44 AM
2+
* angular-timer - v1.1.0 - 2014-05-17 1:35 AM
33
* https://github.com/siddii/angular-timer
44
*
55
* Copyright (c) 2014 Siddique Hameed
@@ -113,38 +113,64 @@ angular.module('timer', [])
113113
function calculateTimeUnits() {
114114

115115
// compute time values based on maxTimeUnit specification
116-
if(!$scope.maxTimeUnit) {
116+
if(!$scope.maxTimeUnit || $scope.maxTimeUnit === 'day') {
117117
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
118118
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
119119
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
120-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
120+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
121+
$scope.months = 0;
122+
$scope.years = 0;
121123
} else if($scope.maxTimeUnit === 'second') {
122124
$scope.seconds = Math.floor($scope.millis / 1000);
123125
$scope.minutes = 0;
124126
$scope.hours = 0;
125127
$scope.days = 0;
128+
$scope.months = 0;
129+
$scope.years = 0;
126130
} else if($scope.maxTimeUnit === 'minute') {
127131
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
128132
$scope.minutes = Math.floor($scope.millis / 60000);
129133
$scope.hours = 0;
130134
$scope.days = 0;
135+
$scope.months = 0;
136+
$scope.years = 0;
131137
} else if($scope.maxTimeUnit === 'hour') {
132138
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
133139
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
134140
$scope.hours = Math.floor($scope.millis / 3600000);
135141
$scope.days = 0;
142+
$scope.months = 0;
143+
$scope.years = 0;
144+
} else if($scope.maxTimeUnit === 'month') {
145+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
146+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
147+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
148+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
149+
$scope.months = Math.floor((($scope.millis / (3600000)) / 24) / 30);
150+
$scope.years = 0;
151+
} else if($scope.maxTimeUnit === 'year') {
152+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
153+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
154+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
155+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24) % 30);
156+
$scope.months = Math.floor(($scope.millis / (3600000)) / 24 / 30);
157+
$scope.years = Math.floor(($scope.millis / (3600000)) / 24 / 30 / 12);
136158
}
137159

138160
// plural - singular unit decision
139161
$scope.secondsS = $scope.seconds==1 ? '' : 's';
140162
$scope.minutesS = $scope.minutes==1 ? '' : 's';
141163
$scope.hoursS = $scope.hours==1 ? '' : 's';
142164
$scope.daysS = $scope.days==1 ? '' : 's';
165+
$scope.monthsS = $scope.months==1 ? '' : 's';
166+
$scope.yearsS = $scope.years==1 ? '' : 's';
143167
//add leading zero if number is smaller than 10
144168
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
145169
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
146170
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
147171
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
172+
$scope.mmonths = $scope.months < 10 ? '0' + $scope.months : $scope.months;
173+
$scope.yyears = $scope.years < 10 ? '0' + $scope.years : $scope.years;
148174

149175
}
150176
//determine initial values of time units and add AddSeconds functionality
@@ -164,6 +190,16 @@ angular.module('timer', [])
164190
$scope.addCDSeconds(extraSeconds);
165191
});
166192
});
193+
194+
$scope.$on('timer-set-countdown-seconds', function(e, countdownSeconds) {
195+
if (!$scope.isRunning) {
196+
$scope.clear();
197+
}
198+
199+
$scope.countdown = countdownSeconds;
200+
$scope.millis = countdownSeconds * 1000;
201+
calculateTimeUnits();
202+
});
167203
} else {
168204
$scope.millis = 0;
169205
}

dist/angular-timer.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ <h3>countdown Time Display according to specified max Time Unit</h3>
193193
&lt;timer countdown="10041" max-time-unit="'minute'" interval="1000"&gt;{{mminutes}} minute{{minutesS}}, {{sseconds}} second{{secondsS}}&lt;/timer&gt;
194194
</code>
195195
</p>
196-
196+
<p class="muted">countdown Time with max time unit option - year</p>
197+
<h3 class="WithMaxTimeUnitAsYear">
198+
<timer end-time="1451606400000" max-time-unit="'year'">{{yyears}} year{{yearsS}}, {{mmonths}} month{{monthsS}}, {{ddays}} day{{daysS}}, {{hhours}} hour{{hoursS}}, {{mminutes}} minute{{minutesS}}, {{sseconds}} second{{secondsS}}</timer>
199+
</h3>
197200
<p class="muted">countdown Time with max time unit option - minute</p>
198201
<h3 class="WithMaxTimeUnitAsMinute">
199202
<timer countdown="10041" max-time-unit="'minute'" interval="1000"> {{mminutes}} minute{{minutesS}}, {{sseconds}} second{{secondsS}}</timer>

0 commit comments

Comments
 (0)