首页 文章

两个日期之间的比较

提问于
浏览
-1

我有一个日历,有以下两种情况:

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

当我点击下个月它应该只移动到“end_month” . 满足这两种情况的条件应该是什么:

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

我已经尝试过比较开始和结束日期对象,但是我可以再多搬一个月,这不应该发生 . 我使用了以下条件:

  • cal_date =星期二2017年01月01日23:58:33 GMT 0530(印度标准时间)

  • $ scope.edate = Wed Nov 15 2017 00:00:00 GMT 0530(印度标准时间)

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) {

1 回答

  • 0

    我个人会创建Date对象:

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

    然后测试:

    if ( current<end )
    

    这可能是最简单的方法 .

相关问题