'use strict';
angular.module('myApp', []).
constant('formatObj', {
"w": 7 * 24 * 60 * 60 * 1000, // milliseconds
"d": 24 * 60 * 60 * 1000,
"h": 60 * 60 * 1000,
"m": 60 * 1000,
"s": 1000,
"ms": 1
})
.controller('mainCtrl', function ($scope) {})
.directive('timeSpanInput', function () {
return {
template: '<input ng-model="time" ng-change="parseTime(time)" ' +
'placeholder="enter time..."></input>(e.g. 3w 4d 12h)<p ng-show="timespan">timespan entered {{timespan | dateSpan}}</p>',
controller: function ($scope, $filter, formatObj) {
$scope.parseTime = function (timeStr) {
var pattern = /(\d+)(\w{1,2})/g, // returns 1w, 1, w from 1w string
match,
timespan = 0;
while (match = pattern.exec(timeStr)) {
console.log(match); // index=1 value 2=format letter w d h s
if (match.length == 3 && isNaN(match[2])) {
timespan += match[1] * formatObj[match[2]];
}
}
//console.log(timespan);
$scope.timespan = timespan;
};
}
};
})
.filter('dateSpan', function (formatObj) {
return function (date) {
// convert milli seconds to weeks days hours minutes seconds string
if (angular.isUndefined(date)) return; // date not defined yet.
var dateObj = {}, // created date obj {w: 3, d: 4, h:12}
dateResult = date, // used to calculated times
factor = 0, // math factor millis to weeks, hours, etc.
result = ''; // formatted result string
for (var key in formatObj) {
factor = formatObj[key];
dateObj[key] = Math.floor(dateResult / factor);
dateResult -= dateObj[key] * factor;
}
// console.log(dateObj);
// generate output string
for (var key in dateObj) {
if (dateObj[key]) {
result += (dateObj[key] + key + ' ');
}
};
//console.log('res', result);
return result;
};
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>
<div ng-app="myApp" ng-contoller="mainCtrl">
<time-span-input></time-span-input>
</div>