本季度的一周,使用javascript

时间:2011-10-14 20:10:02

标签: javascript

我正试图找到一种方法来找到本季度的一周,抵消了2周。我们在本季度的第3个星期六(10.15.11,01.21.12等)进行季度软件发布。有没有办法获得这些信息?

例如,本周将是本季度的第13周。

1 个答案:

答案 0 :(得分:3)

function getQuarterWeek(d) {
    // Convert to UTC to avoid DST changes.
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDay()));
    // For previous Saturday you would normally add one here.
    // But we want to go back two weeks, so subtract 14.
    d.setUTCDate(d.getUTCDate() - 13);
    d.setUTCDate(d.getUTCDate() - d.getUTCDay() - 1);
    // Save the timestamp of this Saturday.
    var s = d.getTime();
    // Now get the first day of the quarter.
    d.setUTCDate(1);
    d.setUTCMonth(d.getUTCMonth() - d.getUTCMonth() % 3);
    // And find the following Saturday.
    d.setUTCDate(7 - d.getUTCDay());
    // Convert the time difference to weeks.
    return (d.getTime() - s) / 604800000 + 1;
}