date-formatter.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * angular-strap
  3. * @version v2.3.9 - 2016-06-10
  4. * @link http://mgcrea.github.io/angular-strap
  5. * @author Olivier Louvignes <olivier@mg-crea.com> (https://github.com/mgcrea)
  6. * @license MIT License, http://www.opensource.org/licenses/MIT
  7. */
  8. 'use strict';
  9. angular.module('mgcrea.ngStrap.helpers.dateFormatter', []).service('$dateFormatter', [ '$locale', 'dateFilter', function($locale, dateFilter) {
  10. this.getDefaultLocale = function() {
  11. return $locale.id;
  12. };
  13. this.getDatetimeFormat = function(format, lang) {
  14. return $locale.DATETIME_FORMATS[format] || format;
  15. };
  16. this.weekdaysShort = function(lang) {
  17. return $locale.DATETIME_FORMATS.SHORTDAY;
  18. };
  19. function splitTimeFormat(format) {
  20. return /(h+)([:\.])?(m+)([:\.])?(s*)[ ]?(a?)/i.exec(format).slice(1);
  21. }
  22. this.hoursFormat = function(timeFormat) {
  23. return splitTimeFormat(timeFormat)[0];
  24. };
  25. this.minutesFormat = function(timeFormat) {
  26. return splitTimeFormat(timeFormat)[2];
  27. };
  28. this.secondsFormat = function(timeFormat) {
  29. return splitTimeFormat(timeFormat)[4];
  30. };
  31. this.timeSeparator = function(timeFormat) {
  32. return splitTimeFormat(timeFormat)[1];
  33. };
  34. this.showSeconds = function(timeFormat) {
  35. return !!splitTimeFormat(timeFormat)[4];
  36. };
  37. this.showAM = function(timeFormat) {
  38. return !!splitTimeFormat(timeFormat)[5];
  39. };
  40. this.formatDate = function(date, format, lang, timezone) {
  41. return dateFilter(date, format, timezone);
  42. };
  43. } ]);