0

i am getting error when day is sunday.monday to saturday its working when day is sunday i am getting error Error: Error while interpolating: {{isOpen(dealer.Day)}} TypeError: Cannot read property 'replace' of undefined

var ds= 'Sun Dec 13 2015 14:04:42 GMT+0530 (India Standard Time)';
      var now = new Date(ds);

if you change day and date its working fine

var ds= 'Sat Dec 12 2015 14:04:42 GMT+0530 (India Standard Time)';
      var now = new Date(ds);

i have added my code below pls help me out.

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

    $scope.isOpen = function(dealerSchedule) {
	var ds= 'Sun Dec 13 2015 14:04:42 GMT+0530 (India Standard Time)';
      var now = new Date(ds);
      //var now = new Date();
      //---if you change day and date you will not get error
      //var ds= 'Sat Dec 12 2015 14:04:42 GMT+0530 (India Standard Time)';
      //var now = new Date(ds);
	  
      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
      }));
	  console.log(nowTime);
      var response = (times == "Leave" ? "Leave" : (nowTime >= new Date(times[0]) && nowTime <= new Date(times[1]) ? "Open Now" : "Closed Now"));
      return response;
    };

	
    $scope.dealers = [{

      S_Email_id: "[email protected] ",
      status: "",
      Store_Name: "Adtiya Samsung Store",
      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:15AM - 04:15PM"
      },
    }, {

      S_Email_id: "[email protected]",
      status: "",
      Store_Name: "sri shakthi mobile service",
      Day: {
        "monday": "09:00 AM - 06:00 PM",
        "tuesday": "09:00 AM - 06:00 PM",
        "wednesday": "09:00 AM - 06:00 PM",
        "thursday": "09:00 AM - 06:00 PM",
        "friday": "09:00 AM - 06:00 PM",
        "saturday": "09:00AM - 06:00PM",
        "sunday": "Leave"
      },

    }, {

      S_Email_id: "[email protected]",
      status: "",
      Store_Name: "sun mobile service center ",
      Day: {
        "monday": "08:30 AM - 07:30 PM",
        "tuesday": "02:30 PM - 07:30 PM",
        "wednesday": "08:30 AM - 07:30 PM",
        "thursday": "08:30 AM - 07:30 PM",
        "friday": "08:30 AM - 07:30 PM",
        "saturday": "08:15 AM - 02:15 PM",
        "sunday": "8:15	AM - 12:15AM"
      },

    }, {

      S_Email_id: "[email protected] ",
      status: "",
      Store_Name: "ragu mobile service center ",
      Day: {
        "monday": "10:00 AM - 10:00 PM",
        "tuesday": "10:00 AM - 10:00 PM",
        "wednesday": "10:00 AM - 04:00 PM",
        "thursday": "10:00 AM - 10:00 PM",
        "friday": "10:00 AM - 10:00 PM",
        "saturday": "leave",
        "sunday": "leave"
      },


    }]
    //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>
    <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>{{dealer.Day}}
      <br>
      <input type="button" value="order" />
          <span>{{isOpen(dealer.Day)}}</span>
      <br>
      <br>
      <br>

    </div>

  </div>
</div>

check my code i am getting errorenter image description here

pls don't give down vote bcoze i dono why its not working i am new to technology.help me out

3 Answers 3

3

The problem is with

now.getDay() - 1 will fail for Sunday (getday() will return 0)

You should put a check for that like similar,

var dayCheck = now.getDay() == 0 ? 6 : now.getDay() - 1;
var times = dealerSchedule[Object.keys(dealerSchedule)[dayCheck]].replace(/(\d\d\:\d\d)(AM|PM)/g, '1/1/1900 $1 $2').split(" - ");
Sign up to request clarification or add additional context in comments.

1 Comment

@martin Updated your fiddle with a few logs - check out the browser logs to see what's going on. If you output now.getDay() - 1 on a Sunday, because now.getDay() returns 0, it outputs NaN, which causes your error. Thumbs up for Amitd's quick fix. Another recommendation is to see if you can change your JSON's data structure so the Days property starts with a Sunday, instead of a Monday.
0

The error is coming from:

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

It's a bit confusing of what you are tying to achieve on this line?

Also as a note for future if you're going to put code into your questions removed commented out code as its not needed.

Also I'd consider upgrading you're angular version.

4 Comments

if day is sunday means i am getting error var ds= 'Sun Dec 13 2015 14:04:42 GMT+0530 (India Standard Time)'; var now = new Date(ds);check my fiddle jsfiddle.net/rpbn6u23/79/.if day is sunday means i am getting error
if its some other day its working pls check this jsfiddle.net/rpbn6u23/80 .if its sunday its not working check jsfiddle.net/rpbn6u23/79
Why are you parsing a date in for now? Why not just have var now = new Date();
Ok I have noticed something... Is there any particular reason why you were doing now.getDay() - 1? Is the desired to check if it is currently open right now and based on Indian timing?
0

Issue with your code is in this statement

now.getDay() - 1

Days in Date object are zero based indices, so on Sunday, getDay() will return 0 and the above statement will evaluate to -1 which is a not valid index in your keys array of your Day object. To look up the day properly from date's getDay(), I suggest you use the index returned by getDay() and change your Day object so that sunday comes first and in sync with the spec like below.

Day: {
        "sunday": "10:15AM - 04:15PM",
        "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"
      }

Also there are few inconsistencies in your JSON object for Day

  1. Sometimes you used leave and sometimes its Leave. It should be same for all Day objects
  2. For the 3rd dealer (sun mobile service center), you mentioned sunday timings as 8:15 AM - 12:15AM while it should be `8:15 AM - 12:15 PM' (from morning 8.15 - afternoon 12.15)

Correcting all these consistencies, adding some new logic for deriving dates from your Day object, here's a working Pen which will display Open Now / Closed Now / Leave accordingly.

Hope this helps :)

1 Comment

Did you see the logic my Pen ? Its a working solution to display status based on dealer schedule & current date & time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.