Global.Date.prototype.getTimespan (param)
return the timespan to current date/time or a different Date object

  • Parameter Object:
    parameter object containing optional properties: .now = String to use if difference is < 1 minute .year|years = String to use for single|multiple year(s) .month|months = String to use for single|multiple month(s) .day|days = String to use for single|multiple day(s) .hour|hours = String to use for single|multiple hour(s) .minute|minutes = String to use for single|multiple minute(s) .date = Date object to use for calculating the timespan
  • Returns
    Object containing properties: .isFuture = (Boolean) .span = (String) timespan
  • See also
    Date.prototype.getAge, Date.prototype.getExpiry
Sourcecode in /home/hannes/helma/modules/core/Date.js:
88:   Date.prototype.getTimespan = function(param) {
89:       if (!param)
90:           param = {date: new Date()};
91:       else if (!param.date)
92:           param.date = new Date();
93:   
94:       var result = {isFuture: this > param.date};
95:       var diff = Math.abs(param.date.diff(this));
96:       var age = {days: Math.floor(diff / Date.ONEDAY),
97:                 hours: Math.floor((diff % Date.ONEDAY) / Date.ONEHOUR),
98:                 minutes: Math.floor((diff % Date.ONEHOUR) / Date.ONEMINUTE)};
99:  
100:      res.push();
101:      if (diff < Date.ONEMINUTE)
102:          res.write(param.now || "now");
103:      else {
104:          var arr = [{one: "day", many: "days"},
105:                     {one: "hour", many: "hours"},
106:                     {one: "minute", many: "minutes"}];
107:          for (var i in arr) {
108:              var value = age[arr[i].many];
109:              if (value != 0) {
110:                  var prop = (value == 1 ? arr[i].one : arr[i].many);
111:                  res.write(value);
112:                  res.write(" ");
113:                  res.write(param[prop] || prop);
114:                  if (i < arr.length -1)
115:                      res.write(param.delimiter || ", ");
116:              }
117:          }
118:      }
119:      result.span = res.pop();
120:      return result;
121:  }