【问题标题】:how to change div content by time of the day?如何按一天中的时间更改 div 内容?
【发布时间】:2011-09-11 01:55:52
【问题描述】:

我希望能够根据用户的时间来改变某个div的内容。 例如,如果是凌晨 5 点,则会显示某些内容。如果是早上 6 点,则会显示另一个内容。

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)

我使用的是http://www.venivortex.com/open-source/jquery-rotator.php,但它不起作用。有人知道类似的吗?

感谢任何帮助!

【问题讨论】:

  • 您能否详细说明您正在使用的旋转器的哪些“不起作用”?
  • 你可以使用服务器端代码吗?
  • 因为它按小时改变 switch_every: "hour" 并且它也不显示任何内容。这是我的尝试:wsul.com/new_build/rotator.html
  • js/jquery.rotator.0.1.js 未找到。尝试指向正确的文件?
  • 确实如此。我不再使用这个示例,因为它只在白天有效,我需要按小时计算,答案有帮助!谢谢

标签: javascript jquery rotation dynamic-content


【解决方案1】:

非常简洁

//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()); 
  }

实时示例:http://jsfiddle.net/9dUJ6/

用 else if 展开

getDate() 返回日期 月份(从 1 到 31)
getDay() 返回星期几(从 0 到 6)
getFullYear() 返回年份(四位数)
getHours() 返回小时(从 0 到 23)
getMilliseconds() 返回毫秒数(0-999)
getMinutes() 返回分钟(从 0 到 59)
getMonth() 返回月份(从 0 到 11)
getSeconds() 返回秒数(从 0 到 59)
getTime() 返回自 1 月 1 日午夜以来的毫秒数, 1970
getTimezoneOffset() 返回 GMT 和本地的时差 时间,以分钟为单位
getUTCDate() 根据通用时间返回月份中的第几天 (1-31)
getUTCDay() 根据通用时间返回星期几 (从0-6)
getUTCFullYear() 根据世界时返回年份 (四位数)
getUTCHours() 根据通用时间返回小时(从 0-23)
getUTCMilliseconds() 返回毫秒数,根据 世界时(0-999)
getUTCMinutes() 根据世界时返回分钟 (从 0-59)
getUTCMonth() 根据世界时返回月份(从 0-11)
getUTCSeconds() 根据世界时返回秒数 (从 0-59)
getYear() 已弃用。请改用 getFullYear() 方法
parse() 解析日期字符串并返回毫秒数 自 1970 年 1 月 1 日午夜起
setDate() 设置月份中的某一天(从 1 到 31)
setFullYear() 设置年份(四位数)
setHours() 设置小时(0-23)
setMilliseconds() 设置毫秒(0-999)
setMinutes() 设置分钟(0-59)
setMonth() 设置月份(0-11)
setSeconds() 设置秒数(0-59)
setTime() 通过添加或减去指定的日期和时间 往返的毫秒数 1970 年 1 月 1 日午夜
setUTCDate() 根据世界时设置月份中的日期 (1-31)
setUTCFullYear() 设置年份,根据世界时(四个 数字)
setUTCHours() 设置小时,根据通用时间(从 0-23)
setUTCMilliseconds() 设置毫秒,根据通用 时间(0-999)
setUTCMinutes() 设置分钟,根据世界时(从 0-59)
setUTCMonth() 根据通用时间设置月份(从 0-11)
setUTCSeconds() 设置秒,根据世界时(从 0-59)
setYear() 已弃用。请改用 setFullYear() 方法
toDateString() 将 Date 对象的日期部分转换为 可读字符串
toGMTString() 已弃用。请改用 toUTCString() 方法
toLocaleDateString() 将 Date 对象的日期部分作为 字符串,使用语言环境约定
toLocaleTimeString() 将 Date 对象的时间部分作为 字符串,使用语言环境约定
toLocaleString() 使用语言环境将 Date 对象转换为字符串 约定
toString() 将 Date 对象转换为字符串
toTimeString() 将 Date 对象的时间部分转换为 字符串
toUTCString() 将 Date 对象转换为字符串,根据 世界时
UTC() 返回日期字符串中的毫秒数 1970 年 1 月 1 日午夜,据 到世界时
valueOf() 返回 Date 对象的原始值

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date 提供日期对象的概述

w3schools 也是如此 http://www.w3schools.com/jsref/jsref_obj_date.asp

【讨论】:

    【解决方案2】:

    this 之类的内容对您来说应该是一个好的开始:

    $(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));    
        };
    })();
    

    【讨论】:

    • 我喜欢那个,乔赛亚!谢谢你。我会试试的。
    • 约西亚,我刚试过。出于某种原因,当我更改小时数时,它一次显示两个。这有什么原因吗? (时间段 2 和时间段 3)
    • @Francheska - 时间范围是多少? (您可以保存小提琴并将链接粘贴到 cmets 中以便我查看)
    • @Francheska - 我再次更新了它,简化了一些事情。另请注意,我的演示错误。范围是排他的。所以range[0] = [1, 6]range[1] = [6, 12] 等不同之处在于下一个范围统计数据与前一个结束时间相同。很高兴我能帮助你!记得接受其中一种解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    相关资源
    最近更新 更多