How to get amCalendar filter in 24hours format (Angular-Moment.js)

↘锁芯ラ 提交于 2019-12-12 02:24:25

问题


I am using angular-moment, and the moment add function: moment().add(7, 'h');.

In order to display when an approval will be done.

I have a table showing estimation for approval for various products. I am using amCalender filter. The results may for example show today at 10pm etc. I want the time to be in 24 hours format instead (today at 22:00).


回答1:


As the docs says, amCalendar:

Format dates using moment.js calendar() method.

You can customize moment calendar output using moment.updateLocale. By default moment use '[Today at] LT', '[Tomorrow at] LT' etc, while in your case you need '[Today at] HH:mm' etc.

Here a live working example:

angular.module('MyApp',['angularMoment'])
.run(function(){
  moment.updateLocale('en', {
    calendar : {
      lastDay : '[Yesterday at] HH:mm',
      sameDay : '[Today at] HH:mm',
      nextDay : '[Tomorrow at] HH:mm',
      lastWeek : '[last] dddd [at] HH:mm',
      nextWeek : 'dddd [at] HH:mm',
      sameElse : 'L'
    }
  });
})
.controller('AppCtrl', function($scope) {
  $scope.message = {};
  $scope.message.time = moment().add(7, 'h');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-moment/1.0.1/angular-moment.min.js"></script>

<div ng-app="MyApp" ng-controller="AppCtrl">
  <span>{{message.time | amCalendar}}</span>
</div>



回答2:


Try this.

moment().add(7, 'h').format('H:mm');


来源:https://stackoverflow.com/questions/42869959/how-to-get-amcalendar-filter-in-24hours-format-angular-moment-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!