Reputation: 31
I want to know if it possible to show and hide an element at a specific time in o´clock ? Let´s say I want the div show up everyday from 20:00 until 07:00.
Here is what I tried but I lack some lines which I have no idea for that. Please help.
function myTimer() {
var d = new Date();
document.getElementById("MyTime").innerHTML = d.toLocaleTimeString();
}
$( document ).ready(function() {
myTimer();
show_hide_me();
});
function show_hide_me () {
<------HERE START TO SHOW/ HIDE (I don´t know how to do it more)
("#MyElm")
if the time between 20:00 - 07:00 show();
else hide();
}
}
Upvotes: 0
Views: 2731
Reputation: 5211
var date = new Date();
var currentHours = date.getHours();
currentHours = ("0" + currentHours).slice(-2);
alert(currentHours);
if (currentHours > 20 || currentHours < 7){
$('#myElem').show();
}
else
{
$('#myElem').hide();
}
Demo:
Upvotes: -1
Reputation: 8954
function show_hide_me () {
var myDate = new Date();
var hours = myDate.getHours();
if (hours > 20 || hours < 7){
$('#myElem').show();
} else {
$('#myElem').hide();
}
}
show_hide_me();
Demo: http://jsfiddle.net/robschmuecker/wkhWK/
Upvotes: 4