datetime_utility.js 2.25 KB
Newer Older
Fatih Acet's avatar
Fatih Acet committed
1 2 3 4 5 6 7 8 9 10
(function() {
  (function(w) {
    var base;
    if (w.gl == null) {
      w.gl = {};
    }
    if ((base = w.gl).utils == null) {
      base.utils = {};
    }
    w.gl.utils.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
11

Fatih Acet's avatar
Fatih Acet committed
12 13 14
    w.gl.utils.formatDate = function(datetime) {
      return dateFormat(datetime, 'mmm d, yyyy h:MMtt Z');
    };
15

Fatih Acet's avatar
Fatih Acet committed
16 17 18
    w.gl.utils.getDayName = function(date) {
      return this.days[date.getDay()];
    };
19 20

    w.gl.utils.localTimeAgo = function($timeagoEls, setTimeago) {
Fatih Acet's avatar
Fatih Acet committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
      if (setTimeago == null) {
        setTimeago = true;
      }
      $timeagoEls.each(function() {
        var $el;
        $el = $(this);
        return $el.attr('title', gl.utils.formatDate($el.attr('datetime')));
      });
      if (setTimeago) {
        $timeagoEls.timeago();
        $timeagoEls.tooltip('destroy');
        return $timeagoEls.tooltip({
          template: '<div class="tooltip local-timeago" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
        });
      }
    };
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

    w.gl.utils.shortTimeAgo = function($el) {
      var shortLocale, tmpLocale;
      shortLocale = {
        prefixAgo: null,
        prefixFromNow: null,
        suffixAgo: 'ago',
        suffixFromNow: 'from now',
        seconds: '1 min',
        minute: '1 min',
        minutes: '%d mins',
        hour: '1 hr',
        hours: '%d hrs',
        day: '1 day',
        days: '%d days',
        month: '1 month',
        months: '%d months',
        year: '1 year',
        years: '%d years',
        wordSeparator: ' ',
        numbers: []
      };
      tmpLocale = $.timeago.settings.strings;
      $el.each(function(el) {
        var $el1;
        $el1 = $(this);
        return $el1.attr('title', gl.utils.formatDate($el.attr('datetime')));
      });
      $.timeago.settings.strings = shortLocale;
      $el.timeago();
      $.timeago.settings.strings = tmpLocale;
    };

70 71 72 73 74 75 76 77
    w.gl.utils.getDayDifference = function(a, b) {
      var millisecondsPerDay = 1000 * 60 * 60 * 24;
      var date1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
      var date2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

      return Math.floor((date2 - date1) / millisecondsPerDay);
    }

Fatih Acet's avatar
Fatih Acet committed
78 79 80
  })(window);

}).call(this);