0

The below code's are getting from moment.js document

moment().date(Number);
moment().date(); // Number
moment().dates(Number);
moment().dates(); // Number

But the input parameter data type is number instead of short month name and year which is mine requirement inputs.

Below is my input format like an array object

`$scope.allMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Act", "Nov", "Dec"];
$scope.Year=2017;// `

So I have month and year, Now I want to get all days and date by using moment.js


Update:

I got the exact result by using normal javascript in my below Answer. But still am unable to find the solution by using moment.js

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

2 Answers2

5
  • Parse the date based on the month and year
  • Loop through the month, formatting each day into an array.

function getMonths(month,year){
    var ar = [];
    var start = moment(year+"-"+month,"YYYY-MMM");
    for(var end = moment(start).add(1,'month');  start.isBefore(end); start.add(1,'day')){
        ar.push(start.format('D-ddd'));
    }
    return ar;
}
console.log(getMonths('Mar',2011))
DanielST
  • 13,783
  • 7
  • 42
  • 65
1

Finally i got it by javascript

//selected year
$scope.selectedYear = function (value) {
            $scope.selectedYearValue = value;// 2011
        }

   //get days and date from  a month and year
        $scope.getDaysArray = function (month) {// month count is 2              
            var names = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
            var date = new Date($scope.selectedYearValue, month - 1, 1);
            $scope.DayAndDate = [];
            while (date.getMonth() == month - 1) {
                result.push({ "Date": date.getDate(), "Day": names[date.getDay()] });
                $scope.DayAndDate.setDate(date.getDate() + 1);
            }
        }

now the result is

js> getDaysArray(2012)
["1-wed", "2-thu", "3-fri", "4-sat", "5-sun", "6-mon", "7-tue",
 "8-wed", "9-thu", "10-fri", "11-sat", "12-sun", "13-mon", "14-tue",
"15-wed", "16-thu", "17-fri", "18-sat", "19-sun", "20-mon", "21-tue", 
"22-wed", "23-thu", "24-fri", "25-sat", "26-sun", "27-mon", "28-tue",
"29-wed"]

but i don't want it by using JavaScript instead of moment.js .

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234