how to change div content by time of the day?
I want to be开发者_如何学JAVA able to change the content of a certain div according to the time of the user. For example, if it's 5am, certain content would show. If it's 6am, another content shows.
John Doe 8am-4pm (changes to that name when its 8am-4pm)
John Doe 5pm-6pm (changes to that name when its 5pm-6pm)
John Doe 7pm-8pm (changes to that name when its 7pm-8pm)
I was using http://www.venivortex.com/open-source/jquery-rotator.php but it doesn't work. Anyone know of something similar?
Any help is appreciated!
very tersely
//initialize date object
var d = new Date();
var currentHour = d.getHours(); //note 0-23
if (currentHour < 6)
{
$('div').append('Before 6am');
}
else if (currentHour == 7)
{
$('div').append('7am');
}
else {
$('div').append('Time Now' + d.getHours() + ':' + d.getMinutes());
}
live example: http://jsfiddle.net/9dUJ6/
expand with else if
getDate() Returns the day of the month (from 1-31)
getDay() Returns the day of the week (from 0-6)
getFullYear() Returns the year (four digits)
getHours() Returns the hour (from 0-23)
getMilliseconds() Returns the milliseconds (from 0-999)
getMinutes() Returns the minutes (from 0-59)
getMonth() Returns the month (from 0-11)
getSeconds() Returns the seconds (from 0-59)
getTime() Returns the number of milliseconds since midnight Jan 1, 1970
getTimezoneOffset() Returns the time difference between GMT and local time, in minutes
getUTCDate() Returns the day of the month, according to universal time (from 1-31)
getUTCDay() Returns the day of the week, according to universal time (from 0-6)
getUTCFullYear() Returns the year, according to universal time (four digits)
getUTCHours() Returns the hour, according to universal time (from 0-23)
getUTCMilliseconds() Returns the milliseconds, according to universal time (from 0-999)
getUTCMinutes() Returns the minutes, according to universal time (from 0-59)
getUTCMonth() Returns the month, according to universal time (from 0-11)
getUTCSeconds() Returns the seconds, according to universal time (from 0-59)
getYear() Deprecated. Use the getFullYear() method instead
parse() Parses a date string and returns the number of milliseconds since midnight of January 1, 1970
setDate() Sets the day of the month (from 1-31)
setFullYear() Sets the year (four digits)
setHours() Sets the hour (from 0-23)
setMilliseconds() Sets the milliseconds (from 0-999)
setMinutes() Set the minutes (from 0-59)
setMonth() Sets the month (from 0-11)
setSeconds() Sets the seconds (from 0-59)
setTime() Sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970
setUTCDate() Sets the day of the month, according to universal time (from 1-31)
setUTCFullYear() Sets the year, according to universal time (four digits)
setUTCHours() Sets the hour, according to universal time (from 0-23)
setUTCMilliseconds() Sets the milliseconds, according to universal time (from 0-999)
setUTCMinutes() Set the minutes, according to universal time (from 0-59)
setUTCMonth() Sets the month, according to universal time (from 0-11)
setUTCSeconds() Set the seconds, according to universal time (from 0-59)
setYear() Deprecated. Use the setFullYear() method instead
toDateString() Converts the date portion of a Date object into a readable string
toGMTString() Deprecated. Use the toUTCString() method instead
toLocaleDateString() Returns the date portion of a Date object as a string, using locale conventions
toLocaleTimeString() Returns the time portion of a Date object as a string, using locale conventions
toLocaleString() Converts a Date object to a string, using locale conventions
toString() Converts a Date object to a string
toTimeString() Converts the time portion of a Date object to a string
toUTCString() Converts a Date object to a string, according to universal time
UTC() Returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time
valueOf() Returns the primitive value of a Date objecti
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date provides and overview of the date object
so too does w3schools http://www.w3schools.com/jsref/jsref_obj_date.asp
Something like this should be a good start for you:
$(function(){
$('#timeperiod1').mood({
range: [1, 7] // hours
});
$('#timeperiod2').mood({
range: [7, 12] // hours
});
$('#timeperiod3').mood({
range: [12, 24] // hours
});
});
// the jquery plugin
// TODO: add end of day re init
// add min/sec along with hours
$.fn.mood = (function(){
var Mood = function(el, opts){
this.el = $(el);
this.range = { bottom: opts.range[0]*1, top: opts.range[1]*1 };
this.init();
};
Mood.prototype = {
init: function(){
this.initTime = this.checkTime(); // 1, 0, -1
this.initTime == 0 ? this.show() : this.hide();
this.start();
},
start: function(){
var t = new Date(),
showDate = new Date(t),
hideDate = new Date(t),
h = t.getHours(), hide = false, show = false;
if(this.initTime < 0) {// time has not yet come
showDate.setHours(this.range.bottom);
showDate.setMinutes(0);
show = true;
}
if(this.initTime <= 0){
hideDate.setHours(this.range.top);
hideDate.setMinutes(0);
hide = true;
}
debugger;
show && setTimeout($.proxy(this.show, this), Math.ceil(showDate.getTime()-t.getTime()));
hide && setTimeout($.proxy(this.hide, this), Math.ceil(hideDate.getTime()-t.getTime()));
},
checkTime: function(){
var t = new Date(), h = t.getHours();
if(h >= this.range.bottom && h <= this.range.top)
return 0;
if(h < this.range.bottom)
return -1;
if(h > this.range.top)
return 1;
},
show: function(){
this.el.show('slow');
},
hide: function(){
this.el.hide('slow');
}
};
return function(opts){
return this.data('rotateMood', new Mood(this, opts));
};
})();
精彩评论