如何使用javascript在特定时间内显示字符串?

时间:2015-07-01 12:28:55

标签: javascript string

我目前正在为我的学校网站制作一个实况横幅。它应包括星期几,日期,时间和当前期间号码。我按照前三个排序,但最后一个给我带来麻烦,因为它只需要在45分钟的时间段内显示。 我昨天改进了我的代码。现在它看起来像this (CODEPEN)。 顺便说一句,感谢昨天帮助过我的所有人! Special thanks to theRoot, for making me have my code simple

<!-->
LIVE DATE AND DAY
<-->
    <style>
        BODY {
            font-family: arial;
        }
    </style>
    <body onload="startTime()">
        <p>
            <span style="font-size:40pt;">TODAY IS A
                <script>
                    var today = new Date();
                    var dd = today.getDate();
                    var ww = today.getDay();
                    var mm = today.getMonth();
                    var yyyy = today.getFullYear();
                    var suffix = ["st","nd","rd","th"];
                    var op = "";
                    var month = ["JANUARY","FEBUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"];
                    var day = ["MONDAY","TUESDAY","WEDNSDAY","THURSDAY","FRIDAY","SATURDAY","SUNDAY"];

                    if(parseInt(dd) > 4)
                        op+=" "+day[ww-1]+" THE "+dd+suffix[3].sup()+" OF ";
                    else
                        op+=" "+day[ww-1]+" THE "+dd+suffix[(parseInt(dd)%10)-1].sup()+" OF ";
                    op+=month[parseInt(mm)-1]+" "+yyyy;
                    document.write(op);
                </script>

                <script>
                    function startTime() {
                        var today=new Date();
                        var h=today.getHours();
                        var m=today.getMinutes();
                        m = checkTime(m);
                        var am = " am";  
                        var pm = " pm";

                        if(h > 12) {
                            h =(h - 12)
                            document.getElementById('time').innerHTML = h+":"+m+pm.sup();
                        } else {
                            document.getElementById('time').innerHTML = h+":"+m+am.sup();
                        }

                        var t = setTimeout(function(){startTime()},500);

                        var period = ["WHY SO EARLY?","BEFORE SCHOOL","PERIOD 1","PERIOD 2","PERIOD 3","PERIOD 4","PERIOD 5","PERIOD 6","PERIOD 7","PERIOD 8","PERIOD 9","PERIOD 10","AFTER SCHOOL","WHY ARE STILL YOU HERE?",];
                    }

                    function checkTime(i) {
                        if (i<10) {i = "0" + i};  // add zero in front of numbers < 10
                        return i;
                    }
                </script>
               <p style="font-size:40pt; display:inline;" id="time"></p>
            </span>
        </p>
    </body>

这就是我现在所拥有的。  感谢。

1 个答案:

答案 0 :(得分:1)

因此,您可以获得有关每个时段的开始时间的参考,然后相应地使用句点数组进行映射。这是一个更新的分支http://codepen.io/anon/pen/eNVdRq

var per=h*60+m;
for(var i=0;i<ptime.length-1;i++){
  if(per>ptime[i]&&per<ptime[i+1]){
    document.getElementById('period').innerHTML=period[i];
    break;
  }
}