0

I'm trying to indicate if a business is open given the current time of day, an open time, and a close time. Here's my time format.

$scope.open = "1970-01-01T13:00:00.000Z"; // We can ignore the dates
$scope.close = "1970-01-02T22:00:00.000Z"

Is there a simple & surefire way to determine this in Angularjs? Something like

<div ng-if="currentTime >= open && currentTime < close"></div>

I have found several moment libraries that could possibly handle this, but I feel like importing a whole library is overkill for a basic time comparison, and most of the javascript solutions I've come across don't handle this time format.

Any input is appreciated!

Clay Banks
  • 4,483
  • 15
  • 71
  • 143
  • 3
    First you will have to convert strong format to js date object. You can achieve this using `new Date('1970-01-01T13:00:00.000Z')` and then set date to today's date before comparison like https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Ashish Oct 22 '17 at 06:36

3 Answers3

1

You can compare dates.

const isStoreOpen = (currentTime, openTime, closedTime) => currentTime >= openTime && currentTime< closedTime;
isStoreOpen(Date.now(), new Date('01 01 1986'), new Date('01  01 2021')); //true
isStoreOpen(Date.now(), new Date('01 01 2018'), new Date('01  01 2021')); //false
Mogadishu
  • 213
  • 2
  • 8
1

You can create a method which directly checks the status of the shop and returns true or false.

$scope.checkShopStatus = function() {
    var open = new Date("1970-01-01T13:00:00.000Z"); // We can ignore the dates
    var close = new Date("1970-01-02T22:00:00.000Z");
    var currentDate = new Date();
    if (currentDate >= open && currentDate <= close) {
        return true;
    }
    return false;
};

Check the running demo code below

var app = angular.module('myApp', []);
app.controller('MyController', ['$scope', function($scope) {
  $scope.title = 'Hello world';

  $scope.checkShopStatus = function() {
    var open = new Date("1970-01-01T13:00:00.000Z"); // We can ignore the dates
    var close = new Date("1970-01-02T22:00:00.000Z");
    var currentDate = new Date();
    if (currentDate >= open && currentDate <= close) {
      return true;
    }
    return false;
  };
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-controller='MyController' ng-app="myApp">
  <div>{{title}}</div>
  <div ng-if="checkShopStatus()">Open</div>
  <div ng-if="!checkShopStatus()">close</div>
</div>
Varun Sukheja
  • 6,170
  • 5
  • 51
  • 93
0

The answers provided were helpful, but I was looking to compare the Times, not the Dates. I ended up using moment.js and formatted my date to a 24-hour string & then comparing

var currentTime = moment(new Date()).format('H:mm');
var openTime = moment(business.hours.sunday.open_time).format('H:mm');
var closeTime = moment(business.hours.sunday.close_time).format('H:mm');

function isStoreOpen(currentTime, openTime, closeTime){   
  if(currentTime >= openTime && currentTime <= closeTime){
    return true;
  }
  return false
}
Clay Banks
  • 4,483
  • 15
  • 71
  • 143