How to get `Month Name` from` Month number` using `moment.js`

I use moment.jsangularjs in the application to convert the date. I want to print Month numberhow Month name. I tried like a roar

<p>{{item.ReviewMonth | date : 'MMMM'}}</p>

where item.ReviewMonthin the numerator.

ex-1. <p>{{5 | date : 'MMMM'}}</p>where 5indicates 'May' but its seal januaryinstead May.

ex-2. <p>{{4 | date : 'MMMM'}}</p>where 4indicates 'April' but its seal januaryinstead April.

How can I get the correct month name from the month number?

+4
source share
4 answers

, date 5 date, 5 1970-01-01T00:00:00.005Z. date January.


:

moment('5', 'M').format('MMMM');

moment('05', 'MM').format('MMMM');

angular.module("app", [])
  .controller("myCtrl", function($scope) {
    $scope.testDate = 5;
    $scope.testDate2 = new Date(5);
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
  <span>{{testDate | date: 'MMMM'}}</span><br>
  {{testDate2}}
</div>
Hide result
+3

, javascript:

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

var month_number = 1;
alert("The current month is " + monthNames[parseInt(month_number)+1]);

, js, :

var formattedMonth = moment('09', 'MM').format('MMMM'); // September

moment(
    '09',           // Desired month
    'MM'            // Tells MomentJs the number is a reference to month
).format('MMMM')    // Formats month as name
+1

momentjs angular, angular-moment.

amParse , , , amDateFormat, .

:

angular.module('MyApp',['angularMoment'])
.controller('AppCtrl', function($scope) {
  $scope.item = {};
  $scope.item.ReviewMonth = 5;
});
<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.18.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">
  <p>{{item.ReviewMonth | amParse:'M' | amDateFormat:'MMMM'}}</p>
</div>
Hide result
+1

, .

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

<div ng-app="myApp" ng-controller="myCtrl">

<any ng-repeat="x in item">
    <my-dir myindex="x.ReviewMonth"></my-dir>
</any>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) 
{
    $scope.item = 
    [
        {
            ReviewMonth:"9"
        },
        {
            ReviewMonth:"2"
        },
        {
            ReviewMonth:"3"
        },
        {
            ReviewMonth:"4"
        },
        {
            ReviewMonth:"5"
        },
        {
            ReviewMonth:"6"
        },
        {
            ReviewMonth:"7"
        },
        {
            ReviewMonth:"7"
        }
    ]
});

app.directive('myDir', function () 
{
  return {
    restrict: 'E',
    scope: {
      myindex: '='
    },
    template:'<div>{{myindex}}</div>',
    link: function(scope, element, attrs)
    {
        var months = ["jan","feb","mar","apr","may","june","july","aug","sep","oct","nov","dec"];
        scope.myindex = months[scope.myindex];
    }
  };
})
</script>

</body>
</html>
0
source

Source: https://habr.com/ru/post/1676433/


All Articles