2 * Core Service - egDate
4 * Date utility functions.
7 angular.module('egCoreMod')
9 .factory('egDate', function() {
14 * Converts an interval string to seconds.
16 * egDate.intervalToSeconds('1 min 2 seconds')) => 62
17 * egDate.intervalToSeconds('2 days')) => 172800 (except across time changes)
18 * egDate.intervalToSeconds('02:00:23')) => 7223
20 service.intervalToSeconds = function(interval) {
22 var start = d.getTime();
23 var parts = interval.split(' ');
25 for(var i = 0; i < parts.length; i += 2) {
28 // interval is a bare hour:min:sec string
29 var times = parts[i].split(':');
30 d.setHours(d.getHours() + Number(times[0]));
31 d.setMinutes(d.getMinutes() + Number(times[1]));
32 d.setSeconds(d.getSeconds() + Number(times[2]));
36 var count = Number(parts[i]);
37 var type = parts[i+1].replace(/s?,?$/,'');
39 if (type.match(/^s/)) {
40 d.setSeconds(d.getSeconds() + count);
41 } else if (type.match(/^min/)) {
42 d.setMinutes(d.getMinutes() + count);
43 } else if (type.match(/^h/)) {
44 d.setHours(d.getHours() + count);
45 } else if (type.match(/^d/)) {
46 d.setDate(d.getDate() + count);
47 } else if (type.match(/^mon/)) {
48 d.setMonth(d.getMonth() + count);
49 } else if (type.match(/^y/)) {
50 d.setFullYear(d.getFullYear() + count);
54 return Number((d.getTime() - start) / 1000);