两次验证之间的角度过滤器不起作用?

时间:2015-12-05 05:06:16

标签: javascript jquery angularjs

我添加了我的完整代码。用户选择的日期是星期三(2015年12月2日星期三)和时间09:00 PM。如果我在周三开放和关闭时间(09:10 AM - 06:30)与商店一起检查PM)。当我点击订单按钮时​​,它应该显示商店现在关闭,而不是现在开放商店。

这里的时间安排一切正常,如果时间是下午意味着它总是显示商店现在打开。有人帮助我 检查以下条件

var d =" Wed Dec 02 2015 09:00 PM" ---它应该显示现在关闭

var d =" Wed Dec 02 2015 06:40 PM" ---它应该显示现在关闭

var d =" Wed Dec 02 2015 09:00 AM" ---它现在应该显示关闭(它工作正常)

var d =" Wed Dec 02 2015 10:00 AM" ---它现在应该显示打开(它工作正常)

demo



angular.module('myApp', [])
  .controller("myCntrl", function($scope, $filter) {

    $scope.isOpen = function(dealerSchedule) {
	
		var d = "Wed Dec 02 2015 09:00 PM",
      	date = new Date(d),
      	times = dealerSchedule[Object.keys(dealerSchedule)[date.getDay() - 1]].split(" - "),
        startTime = new Date(date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + times[0]),
        endTime = new Date(date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + times[1]),
        response = false;
      
      date = $scope.convertToUTC(date);
      startTime = $scope.convertToUTC(startTime);
	  endtime = $scope.convertToUTC(endTime);
	  response = startTime <= date && endTime >= date;
      if(response){
        alert('shop opened now'); 
		  }else{
	      alert('sorry shop closed');
	    }
    };
  
  $scope.convertToUTC = function(date){
    return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
  }

	
    $scope.dealers = [{

       S_Email_id: "aditiya@gmail.com",
        S_Store: "samsung",
        status:"",
        Store_Name: "Adtiya Samsung Store",
        S_Services: "Regular Service,Software Faults,Hardware Faults",
        Store_long_description: "Undertake all kind of samsung mobiles",
        Store_short_description: "Undertake all kind of samsung mobiles",
      Day: {
        "monday": "09:10 AM - 06:30 PM",
        "tuesday": "09:10 AM - 12:00 PM",
        "wednesday": "09:10 AM - 06:30 PM",
        "thursday": "09:10 AM - 06:30 PM",
        "friday": "09:10 AM - 06:30 PM",
        "saturday": "10:15 AM - 04:15 PM",
        "sunday": "10:15 AM - 04:15 PM"
      },
    },  ]
    //var date = new Date();

    //$scope.hhmmsstt = $filter('date')(new Date(), 'hh:mm:ss a');
    //console.log($scope.hhmmsstt);
  })
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="myCntrl">
    <label>Search on Label</label>
    <br>
    <input ng-model="query" type="text" placeholder="Search for name" />
    <br>
    <br>

    <div ng-repeat="dealer in dealers">

      {{dealer.Store_Name}}
      <br>{{dealer.S_Email_id}}
      <br>wednesday :{{dealer.Day.wednesday}}
      <br>
      <input type="button" value="order" ng-click="isOpen(dealer.Day)"/>
          
      <br>
      <br>
      <br>

    </div>

  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要的解决方案是不需要将时间转换为UTC以进行comaparison .. :)

        <html>
            <head>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
            </head>
            <body>
            <div ng-app="myApp">
              <div ng-controller="myCntrl">
                <label>Search on Label</label>
                <br>
                <input ng-model="query" type="text" placeholder="Search for name" />
                <br>
                <br>

                <div ng-repeat="dealer in dealers">

                  {{dealer.Store_Name}}
                  <br>{{dealer.S_Email_id}}
                  <br>wednesday :{{dealer.Day.wednesday}}
                  <br>
                  <input type="button" value="order" ng-click="isOpen(dealer.Day)"/>

                  <br>
                  <br>
                  <br>

                </div>

              </div>
            </div>
            <script>
            angular.module('myApp', [])
              .controller("myCntrl", function($scope, $filter) {

                $scope.isOpen = function(dealerSchedule) {

                    var d = "Wed Dec 02 2015 09:00 PM",
                    date = new Date(d),
                    times = dealerSchedule[Object.keys(dealerSchedule)[date.getDay() - 1]].split(" - "),


                startTime = new Date(date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + times[0]),
                endTime = new Date(date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + times[1]),
                response = false;

                    console.log(endTime+"entime");
                    console.log(startTime+"statrt",date)

                  response = startTime <= date && endTime >= date;
                  if(response){
                alert('shop opened now'); 
                      }else{
                      alert('sorry shop closed');
                    }
                };

              $scope.convertToUTC = function(date){
                return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
              }


                $scope.dealers = [{

                   S_Email_id: "aditiya@gmail.com",
                S_Store: "samsung",
                status:"",
                Store_Name: "Adtiya Samsung Store",
                S_Services: "Regular Service,Software Faults,Hardware Faults",
                Store_long_description: "Undertake all kind of samsung mobiles",
                Store_short_description: "Undertake all kind of samsung mobiles",
                  Day: {
                "monday": "09:10 AM - 06:30 PM",
                "tuesday": "09:10 AM - 12:00 PM",
                "wednesday": "09:10 AM - 06:30 PM",
                "thursday": "09:10 AM - 06:30 PM",
                "friday": "09:10 AM - 06:30 PM",
                "saturday": "10:15 AM - 04:15 PM",
                "sunday": "10:15 AM - 04:15 PM"
                  },
                },  ]
                //var date = new Date();

                //$scope.hhmmsstt = $filter('date')(new Date(), 'hh:mm:ss a');
                //console.log($scope.hhmmsstt);
              })
            </script>
            </body>

            </html>