Supapinzi
Supapinzi

Reputation: 345

Javascript Opening hours countdown

I would like to use an opening time countdown for individual days on my website for a local shop. The shop has opening hours from monday to friday from 09:00 to 18:00 and is open on saturdays from 10:00 to 16:30. I would like to show the users how long the store is open for the day they visit the website. For example a user is on the site on tuesday at 16:30 I want to display something like "Dear customer, our shop is open for another 1,5 hrs.". If the shop is already closed, I would like to display something like "We are currently closed, but you are welcome to visit the store on Monday at 9 o'clock.".

I did a lot of research here and elsewhere, but i just couldn't find a proper solution for this task, so I really hope you could help me.

Upvotes: 0

Views: 1807

Answers (3)

Kai
Kai

Reputation: 3713

Make an array mapping your Openning time, with 0 is Sunday

var openTime = [ {open : -1, close : -1},
                 { open: 9, close : 18 },
                 { open: 9, close : 18 },
                 { open: 9, close : 18 },
                 { open: 9, close : 18 }, 
                 { open: 9, close : 18 },
                 { open: 10, close : 16.5 }
              ]

then check it when user come to page.

var current = new Date();
var day = current.getDay();
var currentTime = current.getHours() + (current.getMinutes()/60);
var remainTime = 0;
if (openTime[day].open >= 0 && openTime[day].open < currentTime && openTime[day].close > currentTime) {
         remainTime= (openTime[day].close  - currentTime).toFixed(2)
}
console.log("the shop will close in %s hours", remainTime);

Upvotes: 1

oo_miguel
oo_miguel

Reputation: 2379

First of all you will need to get the current Date and then get the Day of week, You can do it like that:

var currentdate = new Date();

// this will return a number between 0 and 6 indicating the day of the week.
// starting with 0 for Sunday.
var dayofweek = currentdate.getDay();

And than depending on the day of the week you can calculate the remaining time similar to that:

//e.g Friday:
if(dayofweek==5)
{
    var closedate=new Date();
    closedate.setMinutes(0);
    closedate.setHours(18);

    // this will give you the difference between the current time and opening time.
    var diff = closedate.getTime()-currentdate.getTime();
    var diff_seconds = diff / 1000;
}

Upvotes: 0

Vivek Pradhan
Vivek Pradhan

Reputation: 4857

Since you need to keep track of opening hours based on the day of the week, you will have to get a handle on the current date first like others have pointed out. You could do something like this:

 var d = new Date();
 d.getDay(); //returns the day of the week. Monday is 1 and so on.

Once you have the day of the week, it's just a matter of getting the current time using:

 d.getHours();
 d.getMinutes();
 d.getSeconds();

Once you have the time in this form. You can easily calculate how much time is left for the store to close based on your constraints. Hope this gets you started in the right direction.

Upvotes: 0

Related Questions