all files / src/helpers/ date-formatter.js

100% Statements 23/23
100% Branches 2/2
100% Functions 12/12
100% Lines 23/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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                    214× 211×         214× 98×     214× 114×     600×       214× 101×       214× 101×       214× 98×       214× 101×       214× 98×       214× 101×     214× 9947×        
'use strict';
 
angular.module('mgcrea.ngStrap.helpers.dateFormatter', [])
 
  .service('$dateFormatter', function ($locale, dateFilter) {
 
    // The unused `lang` arguments are on purpose. The default implementation does not
    // use them and it always uses the locale loaded into the `$locale` service.
    // Custom implementations might use it, thus allowing different directives to
    // have different languages.
 
    this.getDefaultLocale = function () {
      return $locale.id;
    };
 
    // Format is either a data format name, e.g. "shortTime" or "fullDate", or a date format
    // Return either the corresponding date format or the given date format.
    this.getDatetimeFormat = function (format, lang) {
      return $locale.DATETIME_FORMATS[format] || format;
    };
 
    this.weekdaysShort = function (lang) {
      return $locale.DATETIME_FORMATS.SHORTDAY;
    };
 
    function splitTimeFormat(format) {
      return /(h+)([:\.])?(m+)([:\.])?(s*)[ ]?(a?)/i.exec(format).slice(1);
    }
 
    // h:mm a => h
    this.hoursFormat = function (timeFormat) {
      return splitTimeFormat(timeFormat)[0];
    };
 
    // h:mm a => mm
    this.minutesFormat = function (timeFormat) {
      return splitTimeFormat(timeFormat)[2];
    };
 
    // h:mm:ss a => ss
    this.secondsFormat = function (timeFormat) {
      return splitTimeFormat(timeFormat)[4];
    };
 
    // h:mm a => :
    this.timeSeparator = function (timeFormat) {
      return splitTimeFormat(timeFormat)[1];
    };
 
    // h:mm:ss a => true, h:mm a => false
    this.showSeconds = function (timeFormat) {
      return !!splitTimeFormat(timeFormat)[4];
    };
 
    // h:mm a => true, H.mm => false
    this.showAM = function (timeFormat) {
      return !!splitTimeFormat(timeFormat)[5];
    };
 
    this.formatDate = function (date, format, lang, timezone) {
      return dateFilter(date, format, timezone);
    };
 
  });