知識社群Login
值日生的 javascript 小程式
by 系統管理者, 2010-06-12 11:42, Views(1979)
使用自由欄位,將下面的程式放進去,並修改 schedule, members 就可以了
 
-------------------------------------------------------------------
<script type="text/javascript">
var schedule = {year:2010, month:6, day:12, duration:7, number:2};
var members = ["Rick", "Nash", "yvonne", "tcsu","irenetsung","lao1206","linchuhs"];

var info = getInfo();
// document.write("<div style='border:1px solid red; padding:2px'>");
// document.write("值日生: ");
document.write("<span style='color:red'>" + info.members + "</span>");
document.write(" <span style='font-size:11px; color:#aaa'>(" + info.duration + ", 下次: " + info.next + ")</span> <p>");
document.write("麻吉值日生公約→ 我最棒: <br>");
document.write("肩負起Po文、回討論、關心新麻吉的責任喲 ... 就是幫助大家都能互相學習交流~");
// document.write("</div>");
 
 
 
// 以下是程式碼,請不要理我 ....
function getInfo()
{
    var baseDay = new Date(schedule.year, schedule.month - 1, schedule.day);
    var today = new Date();
    var times = Math.floor(NumOfDays(baseDay, today) / schedule.duration);
    var dStart = Math.floor(NumOfDays(baseDay, today) / schedule.duration) * schedule.duration;
    var dEnd = dStart + schedule.duration;
    var duration = {start: nextDate(baseDay, dStart), end: nextDate(baseDay, dEnd)};
    var memberStart = (times * schedule.number) % members.length;   
    var str = "";
    var nextStr = "";
    var i, j;
    for (i=memberStart; i<memberStart + schedule.number; i++)
    {
        if (str != "") str += ", ";
        str += members[i % members.length];
    }
    for (j=i; j<i + schedule.number; j++)
    {
        if (nextStr != "") nextStr += ", ";
        nextStr += members[j % members.length];
    }
    return {members: str, duration: "~ " + formatDate(duration.end), next: nextStr};
}
function nextDate(currDate, day) { return new Date(currDate.getTime() + day * 1000*60*60*24); }
function formatDate(d)           { return d.getFullYear() + "." + (d.getMonth() + 1) + "." + d.getDate(); }
function NumOfDays(d1, d2)       { return Math.ceil( (d2.getTime()-d1.getTime()) / (1000*60*60*24) ); }
</script>