开发者

Week of the quarter, using javascript

开发者 https://www.devze.com 2023-04-13 07:30 出处:网络
I\'m trying to come up with a way to find the week of the quarter, offset by 2 weeks. We do a quarterly software release, on the 3rd Saturday of the quarter (10.15.11, 01.21.12, etc.). Is there way to

I'm trying to come up with a way to find the week of the quarter, offset by 2 weeks. We do a quarterly software release, on the 3rd Saturday of the quarter (10.15.11, 01.21.12, etc.). Is there way to get that information?

For example, this week would b开发者_高级运维e the 13th week of the quarter.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号