-1

I have a calender and has following two scenarios:

case - 1

  • current_month = 8 (aug)
  • end_month = 10 (oct)
  • current_year = 2017
  • end_year = 2017

case - 2

  • current_month = 8 (aug)
  • end_month = 2 (feb)
  • current_year = 2017
  • end_year = 2018

When I click next month it should move only until "end_month". What should be the condition to satisfy both the cases:

if(condition)
  alert("Move to next month");
else
  alert("condition fail");

I have tried comparing start and end date objects, But I'm able to move one more month extra, which should not happen. I have used the following conditions:

  • cal_date = Tue Aug 01 2017 23:58:33 GMT+0530 (India Standard Time)
  • $scope.edate = Wed Nov 15 2017 00:00:00 GMT+0530 (India Standard Time)

    if (cal_date.getMonth() > $scope.edate.getMonth() + 1 || cal_date.getMonth() == $scope.edate.getMonth() || $scope.edate.getFullYear() <= new Date().getFullYear()) 
    
    if (cal_date.getMonth() > $scope.edate.getMonth() || cal_date.getMonth() == $scope.edate.getMonth()) 
    
    if (cal_date.getTime() < new Date(new Date($scope.edate) - 1)) {
    
    if (cal_date < $scope.edate) {
    
Shaik Nizamuddin
  • 589
  • 1
  • 7
  • 23
  • Are you wanting to move to the month that is shown for `end_month` when the user clicks next? – Ohjay44 Aug 11 '17 at 17:13
  • 2
    Why the PHP tag? Show what you tried already, even if it isn't working. – Qirel Aug 11 '17 at 17:20
  • if(current_month < end_month || current_year < end_year) – Zachooz Aug 11 '17 at 17:22
  • Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Rob W Aug 11 '17 at 17:35
  • Try using [Moment.js](https://momentjs.com/) – Kyle Krzeski Aug 11 '17 at 17:38
  • 1
    I voted to close the question because it doesn't show any code. Also, "I have tried comparing start and end date objects, But I'm able to move one more month extra" implies that you have code which is written but flawed; It is necessary to see that code to know why it doesn't work properly. – Claies Aug 11 '17 at 17:47
  • @Ohjay44 : No i want to move to next month and next month till the end month ,If end month reaches the condition should become false and should not move to next month. – Shaik Nizamuddin Aug 11 '17 at 18:35

1 Answers1

0

Personally I would create Date objects:

var current = new Date(year,month)
var end = new Date(year,month)

and then test:

if ( current<end )

That would probably be the simplest approach.

Octopus
  • 8,075
  • 5
  • 46
  • 66