Last active
February 2, 2020 02:52
-
-
Save yuru4c/7f302cf9f587ca1f8182eb84fd9c26df to your computer and use it in GitHub Desktop.
日付から週番号を求めます
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 週番号を求めます | |
* @function | |
* @param time 週番号を求める日付 | |
* @param {number} [weekRule=0] 週番号の規則 | |
* <br> 0 - 元日から大晦日まで | |
* <br> 1~7 - その日を含む週から | |
* @param {number} [firstDay=0] 週初日の曜日 | |
* @returns {number} 週番号 (0 - エラー) | |
*/ | |
var weekOfYear = (function (date) { | |
var week = date.setDate(8) - date.setDate(1); | |
return function (time, weekRule, firstDay) { | |
var d = new Date(time); | |
var w = +weekRule, f = +firstDay; | |
var end = | |
(d.getDate() - (w || 1) + 7) - | |
(d.getDay() - (f || 0) + 7) % 7; | |
if (w) d.setDate(end); | |
var year = date.setFullYear(d.getFullYear()); | |
if (!w) d.setDate(end); | |
var num = (d - year) / week; | |
return num >= 0 ? -~num : 0; | |
}; | |
})(new Date(0, 0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment