1

我想做的是根据日期显示一年中每周的 jpg。有谁知道为什么这不起作用?

Date.prototype.getWeek = function() {         
var onejan = new Date(this.getFullYear(),0,1);         
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);     
}       

var weekNumber = (new Date()).getWeek();

var test='<img src="C:/temp/ + weekNumber + .jpg"/>';
document.write(test);
4

1 回答 1

1

这是一个获取周数的函数。

function getWeekNumber(d) {
    // Copy date so don't modify original
    d = new Date(d);
    d.setHours(0,0,0);
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday's day number 7
    d.setDate(d.getDate() + 4 - (d.getDay()||7));
    // Get first day of year
    var yearStart = new Date(d.getFullYear(),0,1);
    // Calculate full weeks to nearest Thursday
    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)
    // Return array of year and week number
    return weekNo;
}

学分:RobG

使用上面的函数来获取数字并在你的脚本中使用

weekNumber = getWeekNumber(new Date());
var test='<img src="C:/temp/' + weekNumber + '.jpg"/>';
document.write(test);

演示

于 2012-10-08T05:31:30.170 回答