0

I want to show all the dates of a given month. Suppose, if I select Oct, 2015, then it should show all the dates of that month in a list. I can print all the dates of next 365 days using setDate(), but how can I print only dates of a selected month. Check this code to show next 365 days.

function Ctrl($scope) {
  $scope.dates = [];
  for (i = 0; i <= 364; i++) {
    var d = new Date();
    $scope.dates.push(d.setDate(d.getDate() + i));
  }
}
cyberoy
  • 363
  • 2
  • 7
  • 18

5 Answers5

0

You can use DateJs

and your code could look like that

function Ctrl($scope) {
  $scope.dates = [];
  for (i = 0; i < 365; i++) {
    $scope.dates.push((i).days().fromNow());
  }
}
Gilles Bodart
  • 594
  • 1
  • 10
  • 27
0

If you can get total days of a month of a year then you can done

 $scope.month =10; $scope.year =2015;  $scope.nDays = new Date( $scope.year,  $scope.month, 0).getDate() ;

The above code get nDays of Oct of Year 2015.

Loop nDays with following code

for (i = 1; i <= $scope.nDays; i++) {
var d = new Date();
$scope.dates.push(d.setDate(d.getDate() + i));}

Hope it helps.

Lewis Hai
  • 1,114
  • 10
  • 22
  • Hi, can you please look at fiddle :- http://jsfiddle.net/abhijitloco/2cpc3gp3/ It's not working. – cyberoy Oct 19 '15 at 09:18
  • Thanks. Please have a look here. It is showing 30 days for Oct and incase of Nov it is also taking Dec 01 extra. http://jsfiddle.net/abhijitloco/2cpc3gp3/3/ – cyberoy Oct 19 '15 at 09:46
0

You are very close. As Jaromanda X suggested, just start from 1 and keep going until the month changes (and don't forget to keep variables local). You'll need to copy the date each time:

function Ctrl($scope) {
  $scope.dates = [];
  var d = new Date(),
      i = 1,
      m = d.getMonth();

  // Set date to start of month
  d.setDate(i);

  // Store the current month and keep going until it changes
  while (d.getMonth() == m) {

    // Store dates as a string (format however you wish)
    $scope.dates.push('' + d);

    // Or store dates as Date objects
    $scope.dates.push(new Date(+d));

    // Increment date
    d.setDate(++i);
  }
  // return something?
}

Edit

Allow entry of month (also example of do rather than for loop):

// Use calendar month number for month, i.e. 1=jan, 2=feb, etc.
function Ctrl($scope, month) {
  $scope.dates = [];
  var d = new Date();
  d.setMonth(month - 1, 1);

  do {
    $scope.dates.push('' + d);
    d.setDate(d.getDate() + 1);
  } while (d.getDate() != 1)
}

Or allow entry of month and year with defaults of current month and year:

function Ctrl($scope, month, year) {
  $scope.dates = [];
  var d = new Date();
  d.setFullYear(+year || d.getFullYear(), month - 1 || d.getMonth(), 1);

  do {
    $scope.dates.push('' + d);
    d.setDate(d.getDate() + 1);
  } while (d.getDate() != 1)
}
RobG
  • 142,382
  • 31
  • 172
  • 209
  • That's great. It is taking the current month and showing the dates. If I want to change the month at any time then? Suppose, want to get date of Nov. – cyberoy Oct 19 '15 at 09:31
  • Add a parameter for the month (see updated post). You might also add a parameter for year with defaults to current month and year. – RobG Oct 19 '15 at 09:38
  • Please have a look at fiddle. Your last two code part is not working. http://jsfiddle.net/abhijitloco/2cpc3gp3/4/ – cyberoy Oct 19 '15 at 09:59
  • You need to change the controller to accept arguments, see [*Can you pass parameters to an AngularJS controller on creation?*](http://stackoverflow.com/questions/14523679/can-you-pass-parameters-to-an-angularjs-controller-on-creation). – RobG Oct 19 '15 at 11:56
0

Inject the moment js library it will help you alot for this purpose

Here is the link http://momentjs.com/

VizardCrawler
  • 1,343
  • 10
  • 16
-1

You can try this like this:

function Ctrl($scope) {
  $scope.dates = [];
  var month = 3;
  var year = 2015;
  for (i = 1; i <= 30; i++) { //You have to know the number of days in the month you want tho
    var d = new Date();
    $scope.dates.push(new Date(year, month, i));
  }
}
dekajoo
  • 2,024
  • 1
  • 25
  • 36