如何在店铺时间检查选择的时间是否匹配?

时间:2015-12-03 11:03:27

标签: javascript jquery angularjs

我是programmng和angularjs的新手。有些人帮我解决这个问题。我需要检查给定的时间在商店开放和关闭时间之间匹配。我从日期获得日期并且我已经过了时间。我已经尝试了一些代码但是它不起作用。当我点击按钮时它应该检查商店数据并显示一些基于status的消息。现在我得到无效的日期行没有43.任何一个请帮我下面我给我的条件

Day: {
        "monday": "09:10AM - 06:30PM",
        "tuesday": "09:10AM - 12:00PM",
        "wednesday": "09:10AM - 06:30PM",
        "thursday": "09:10AM - 06:30PM",
        "friday": "09:10AM - 06:30PM",
        "saturday": "10:15AM - 04:15PM",
        "sunday": "10:15AM - 04:15PM"
      },

例如。 1.如果我通过日期(2015年12月2日星期三00:00:00格林尼治标准时间+ 0530(印度标准时间)/ 2015年12月12日/星期三),如果我超过时间上午11:00,我需要检查商店日(这是上面提到的。星期三和时间上午11点,商店开放和关闭时间之间的匹配,所以我需要显示警报商店打开。

2.如果我通过日期(2015年12月3日星期五00:00:00 GMT + 0530(印度标准时间)/ 03/12/2015 /星期四),如果我通过时间08:00 pm,我需要检查购物日(上面提到)。星期四和晚上08:00的时间在商店营业时间和营业时间之间不匹配所以我需要显示提醒商店已关闭或离开演示

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

    $scope.isOpen = function(dealerSchedule) {
	console.log(dealerSchedule);
      
	  var udate='Wed Dec 02 2015 00:00:00 GMT+0530 (India Standard Time)';
	  var now = new Date(udate);
	  console.log(now);
	  var times = dealerSchedule[Object.keys(dealerSchedule)[now.getDay() - 1]].replace(/(\d\d\:\d\d)(AM|PM)/g, '1/1/1900 $1 $2').split(" - ");
     
	  //var nowTime = new Date('1/1/1900 ' + now.toLocaleTimeString(navigator.language, {
        //hour: '2-digit',
        //minute: '2-digit',
        //hour12: true
      //}));
	  var ntime ='11:00am';
	  var nowTime = new Date(ntime);
	  console.log(nowTime);
      var response = (times == "Leave" ? "Leave" : (nowTime >= new Date(times[0]) && nowTime <= new Date(times[1]) ? "Open Now" : "Closed Now"));
     
	  if(response =='Closed Now' || response =='Leave' )
	  {
	  alert('sorry shop closed');
	  }else
	  {
	  alert('shop opened now');
	  }
	  
	  
    };

	
    $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:10AM - 06:30PM",
        "tuesday": "09:10AM - 12:00PM",
        "wednesday": "09:10AM - 06:30PM",
        "thursday": "09:10AM - 06:30PM",
        "friday": "09:10AM - 06:30PM",
        "saturday": "10:15AM - 04:15PM",
        "sunday": "10:15AM - 04:15PM"
      },
    },  ]
    //var date = new Date();

    //$scope.hhmmsstt = $filter('date')(new Date(), 'hh:mm:ss a');
    //console.log($scope.hhmmsstt);
  })
//]]>
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.1/angular.min.js'></script>
  
  
  
<div ng-app="myApp">
  <div ng-controller="myCntrl">
    <label>Search on Label</label>
    <br>
  
    <br>
    <br>

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

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

    </div>

  </div>
</div>

1 个答案:

答案 0 :(得分:0)

首先,您将商店营业时间设置为1900年

var times = dealerSchedule[Object.keys(dealerSchedule)[now.getDay() - 1]].replace(/(\d\d\:\d\d)(AM|PM)/g, '1/1/1900 $1 $2').split(" - ");

然后你得到了今天的日期

var nowTime = new Date(ntime);

你正在检查今天的日期是否在1900年的两次之间。

var response = (times == "Leave" ? "Leave" : (nowTime >= new Date(times[0]) && nowTime <= new Date(times[1]) ? "Open Now" : "Closed Now"));

底线是你要比较日期,而不想比较时间。