Angular Learner
Angular Learner

Reputation: 390

Need to display dates and days in a month using moment js

I need help in moment js. I have this piece of code which gives me months name and days and dates in it.

    vm.displayDays = [];
    vm.monthsList = [];
    initController();

    function initController() {
        vm.displayMoment =  moment().format('MMMM Do YYYY, h:mm:ss a');
        vm.monthsList = moment.monthsShort();
        for(var i = 0; i < vm.monthsList.length; i++) {
            getMonths(vm.monthsList[i], 2016);
        }
    }

    function getMonths(month,year){
        var start = moment(year+"-"+month,"YYYY-MMM");
        for(var end = moment(start).add(1,'month');  
            start.isBefore(end); start.add(1,'day')){
            vm.displayDays.push(start.format('D-ddd'));
        }
        return vm.displayDays;
    }

Here is the output of it Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1-Fri 2-Sat 3-Sun 4-Mon 5-Tue 6-Wed 7-Thu 8-Fri 9-Sat 10-Sun 11-Mon 12-Tue

I need the output in this way Jan and below i need its corresponding days and dates

 Jan
 01-Mon
 02-tue

In this way, can please somebody help me in achieveing this one.

Upvotes: 1

Views: 839

Answers (1)

leroydev
leroydev

Reputation: 2945

This seems to be what you're looking for:

initController();

function initController() {
  var months = moment.monthsShort();
  for (var i = 0; i < months.length; i++) {
    var currentMonth = months[i];
    $("#content").append('<h3>' + currentMonth + '</h3>');
    printDates(currentMonth, 2016);
  }
}

function printDates(month, year) {
  var start = moment(year + "-" + month, "YYYY-MMM");
  for (var end = moment(start).add(1, 'month'); start.isBefore(end); start.add(1, 'day')) {
    $("#content").append('<p>' + start.format('D-ddd') + '</p>');
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content">

</div>

Upvotes: 0

Related Questions