Highlight current day

In a website for a restaurant in the footer section I have the opening times like this:

				<div class="col-md-4 margin-b-30 wow fadeInUp" data-wow-delay="1.5s">
					<h3>Openingstijden</h3>
                    <ul class="openingtijden">
                    	<li>Maandag<span>12:00 - 22:00 uur</span></li>
                        <li>**Dinsdag<span>Closed</span>**</li>
                        <li>Wonsdag<span>12:00 - 22:00 uur</span></li>
                        <li>Donderdag<span>12:00 - 22:00 uur</span></li>
                        <li>Vrijdag<span>16:00 - 22:00 uur</span></li>
                        <li>Zaterdag<span>16:00 - 22:00 uur</span></li>
                        <li>Zondag<span>16:00 - 22:00 uur</span></li>
                    </ul>
				</div>

What I would like to accomplish is that the current day will be highlighted (bold and/or other color). I.e. Today is Tuesday so the word closed should be highlighted, and tommorow Wednesday and so on.

Is this doable with Javascript or should I use PHP ? Thank you in advance

Yes it is doable with JavaScript.
Examine Date object in JavaScript and check for current date and highlight it by adding some CSS class to li element or however you want to accomplish this.

Are you loading any javascript helper frameworks?

jQuery:

$('.openingtijden li')[new Date().getDay()+6%7].addClass('highlightdate')

normal JavaScript

document.getElementsByClassname('openingtijen')[0].children()[new Date().getDay()+6%7].classlist.add('highlightdate')

(The math is necessary because Javascript defines a week as being 0-6, with 0 being sunday; to shift it to being Monday = 0 [My dutch isnt good, but i assume thatā€™s what youā€™re doing], add 6 and modulo by 7.)

Yes I use jQuery!

Hi m_hutley. I tried both options, but neither is doing something:

CSS

<style>
.highlightdate
{
	color: #FF0000;	
}
</style>

Javascript

document.getElementsByClassname('openingtijden')[0].children()[new Date().getDay()+6%7].classlist.add('highlightdate');

What can be the reason?

Are you putting that line after the LIā€™s have been loaded, or before? Are there any errors in the javascript console?

It is at the bottom of the html., should I add it in the head?

Nope. Bottomā€™s the best place for it. That or wrapped in a $(document).ready()

The consoleā€™s not spitting anything out?

This is what the console spitting :slight_smile:

Uncaught TypeError: document.getElementsByClassname is not a function

Thank you for the patience

Capitalize the n in ClassName. I got lazy.

I changed it Accordingly

<script>
$(document).ready(function(){
	document.getElementsByClassName('openingtijden')[0].children()[new Date().getDay()+6%7].classlist.add('highlightdate');
});
</script> 

But still nothing :frowning:

Eventually iā€™ll stop being lazy, I swear. Maybe when itā€™s not 6 AM.

$(document).ready(function(){
	document.getElementsByClassName('openingtijden')[0].children[new Date().getDay()+6%7].classList.add('highlightdate');
});

or

$(document).ready(function(){
	$('.openingtijden li')[(new Date().getDay()+6%7)].addClass('highlightdate')
});

eitherā€¦ should work.

Hahahaha. Iā€™m realy not sure what Iā€™m doing wrong her is the HTML:

<!doctype html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Css stylesheets -->

<link href="css/plugins.css" rel="stylesheet">
<link href="css/site.css" rel="stylesheet">

<style>
.highlightdate
{
	color: #FF0000;	
}
</style>
<title>Chinees fusion restaurant in Haarlem | De Draak</title>
</head>
<body>
 	<footer class="footer">
		<div class="container">
			<div class="row">
				<div class="col-md-4 margin-b-30 pr-5 wow fadeInUp" data-wow-delay="0.5s">
					<h3>Filosofie</h3>
                    <p>Lekkere smaken, gezonde ingrediƫnten en eindeloos genieten daar draait het om bij restaurant De Draak. Bij De Draak zijn onze klanten onze vrienden. Het begint al bij binnenkomst met onze vriendelijke bediening en warme atmosfeer waarbij elke gast zich meteen thuis voelt.</p><p>Dezelfde zorg die aan de sfeer wordt besteed hanteren we ook voor het eten. Chef Fan streeft naar culinaire hoogstandjes door veel aandacht te besteden aan de bron en de kwaliteit van de ingrediƫnten.</p>
				</div>
                <div class="col-md-4 margin-b-30 wow fadeInUp" data-wow-delay="1.0s">
                	<h3>Contact informatie</h3>
               		<div class="info">
                    	<div>
                        	<i class="fa fa-map-marker" aria-hidden="true"></i>
                            <p>Amsterdamstraat 24-26<br>2032 PP Haarlem</p>
                        </div>
                    	<div>
                        	<i class="fa fa-envelope" aria-hidden="true"></i>
                            <p>info@dedraakhaarlem.nl</p>
                        </div> 
                    	<div>
                        	<i class="fa fa-phone" aria-hidden="true"></i>
                            <p>+31 (0)6 4181 8579</p>
                        </div>                    
                    </div>
                </div>
				<div class="col-md-4 margin-b-30 wow fadeInUp" data-wow-delay="1.5s">
					<h3>Openingstijden</h3>
                    <ul class="openingtijden">
                    	<li>Maandag<span>12:00 - 22:00 uur</span></li>
                        <li>Dinsdag<span>Closed</span></li>
                        <li>Wonsdag<span>12:00 - 22:00 uur</span></li>
                        <li>Donderdag<span>12:00 - 22:00 uur</span></li>
                        <li>Vrijdag<span>16:00 - 22:00 uur</span></li>
                        <li>Zaterdag<span>16:00 - 22:00 uur</span></li>
                        <li>Zondag<span>16:00 - 22:00 uur</span></li>
                    </ul>
				</div>
			</div>
		</div>
	</footer>   
    
<script src="js/plugins.js"></script>
<script src="js/main.js"></script> 
<script>
$(document).ready(function(){
	$('.openingtijden li')[(new Date().getDay()+6%7)].addClass('highlightdate')
	//document.getElementsByClassName('openingtijden')[0].children[new Date().getDay()+6%7].classList.add('highlightdate');
});
</script> 
</body>
</html>

For the convenience I just left the footer section in! I tried both

Right. Different tack. More math. Actually remembering that modulo is a division operator and has a higher order of precedence.

$(document).ready(function(){
	$('.openingtijden li:nth-child('+(((new Date().getDay()+6)%7)+1)+')').addClass('highlightdate')
});

Mr . m_hutley, youā€™re hero :grinning: This is working great :+1:. Thank you again for the patience. Now you can take some rest :wink:

1 Like

It depends. Do you want time based on a users clock or the server clock?

You could use ā€œUTCā€ if it isnā€™t critical if a user mucks with their client-side.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.