The other answer is very good. This helped me create a demo, because without it I would have missed this moment with a month (from 0 to 11).
But you do not need to create the meridiem function, because it is already implemented in moment.js . The fact is that the use of documents is not very clear from the documents.
You can use the meridiem function as follows:
var curDate = moment().hour(yourHour);
If you want a boolean string instead of the string AM/PM , you can pass the string AM / PM to curDate.localeData().isPM(meridiem) . This will return true or false.
A point with a month that you do not want to display from 0 to 11 in your chosen one can be fixed using the displayLabel function, which will increase each value of the month array. Then you will see that it is displayed from 1 to 12, but saved as from 0 to 11. If you write a directive that can be better handled by the $parser / $formatter ngModel .
Please see the demo below or on this jsfiddle .
angular.module('demoApp', []) .filter('momentDate', MomentDateFilter) .filter('momentUTC', MomentUTCFilter) .controller('MainController', MainController); function MomentDateFilter() { return function(input, format) { return moment(input).format(format); }; } function MomentUTCFilter() { return function(input, format) { return moment.utc(input).format(format); }; } function MainController($scope, $log) { var vm = this, now = moment(); vm.checkHour = checkHour; vm.dateSelect = dateSelection(); vm.displayLabel = displayLabel; vm.now = now; vm.selected = { "day": 27, "month": 8, "year": 2015, "hour": 18, "meridiem": "PM", "minutes": 6, "seconds": 20 }; function dateSelection() { return {
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.0/angular.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script> <div ng-app="demoApp" ng-controller="MainController as mainCtrl" class="container-fluid"> <form class="form-inline"> <div ng-repeat="(key, array) in mainCtrl.dateSelect" class="form-group"> <label>{{key}}</label><select class="form-control" ng-change="mainCtrl.checkHour(key, array[mainCtrl.selected[key]])" ng-model="mainCtrl.selected[key]" ng-options="value as mainCtrl.displayLabel(key, value) for value in mainCtrl.dateSelect[key]" ng-disabled="key === 'meridiem'"> </select> </div> </form> selected: <pre>{{mainCtrl.selected|json}}</pre> raw date (unformatted, UTC): {{mainCtrl.selected | momentUTC}}<br/> date formatted (meridiem locale): {{mainCtrl.selected | momentUTC : 'LLLL' }}<br/> now (momentjs) {{mainCtrl.now}} </div>
Awolf source share