2026-07-05 06:41:32 +00:00
"use strict" ;
Object . defineProperty ( exports , "__esModule" , {
value : true
} ) ;
exports . default = getWeekYear ;
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
var _index = _interopRequireDefault ( require ( "../startOfWeek/index.js" ) ) ;
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
var _index2 = _interopRequireDefault ( require ( "../toDate/index.js" ) ) ;
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
var _index3 = _interopRequireDefault ( require ( "../_lib/toInteger/index.js" ) ) ;
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
var _index4 = _interopRequireDefault ( require ( "../_lib/requiredArgs/index.js" ) ) ;
2026-07-07 07:11:50 +00:00
function _interopRequireDefault ( obj ) { return obj && obj . _ _esModule ? obj : { default : obj } ; }
2026-07-05 06:41:32 +00:00
/**
* @name getWeekYear
* @category Week-Numbering Year Helpers
* @summary Get the local week-numbering year of the given date.
*
* @description
* Get the local week-numbering year of the given date.
* The exact calculation depends on the values of
* `options.weekStartsOn` (which is the index of the first day of the week)
* and `options.firstWeekContainsDate` (which is the day of January, which is always in
* the first week of the week-numbering year)
*
* Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
*
2026-07-07 07:11:50 +00:00
* ### v2.0.0 breaking changes:
*
* - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
*
2026-07-05 06:41:32 +00:00
* @param {Date|Number} date - the given date
* @param {Object} [options] - an object with options.
* @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
* @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
* @param {1|2|3|4|5|6|7} [options.firstWeekContainsDate=1] - the day of January, which is always in the first week of the year
* @returns {Number} the local week-numbering year
* @throws {TypeError} 1 argument required
* @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
* @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
*
* @example
* // Which week numbering year is 26 December 2004 with the default settings?
* const result = getWeekYear(new Date(2004, 11, 26))
* //=> 2005
*
* @example
* // Which week numbering year is 26 December 2004 if week starts on Saturday?
* const result = getWeekYear(new Date(2004, 11, 26), { weekStartsOn: 6 })
* //=> 2004
*
* @example
* // Which week numbering year is 26 December 2004 if the first week contains 4 January?
* const result = getWeekYear(new Date(2004, 11, 26), { firstWeekContainsDate: 4 })
* //=> 2004
*/
function getWeekYear ( dirtyDate , options ) {
2026-07-07 07:11:50 +00:00
var _options$locale , _options$locale$optio ;
2026-07-05 06:41:32 +00:00
( 0 , _index4 . default ) ( 1 , arguments ) ;
var date = ( 0 , _index2 . default ) ( dirtyDate ) ;
var year = date . getFullYear ( ) ;
2026-07-07 07:11:50 +00:00
var localeFirstWeekContainsDate = options === null || options === void 0 ? void 0 : ( _options$locale = options . locale ) === null || _options$locale === void 0 ? void 0 : ( _options$locale$optio = _options$locale . options ) === null || _options$locale$optio === void 0 ? void 0 : _options$locale$optio . firstWeekContainsDate ;
var defaultFirstWeekContainsDate = localeFirstWeekContainsDate == null ? 1 : ( 0 , _index3 . default ) ( localeFirstWeekContainsDate ) ;
var firstWeekContainsDate = ( options === null || options === void 0 ? void 0 : options . firstWeekContainsDate ) == null ? defaultFirstWeekContainsDate : ( 0 , _index3 . default ) ( options . firstWeekContainsDate ) ; // Test if weekStartsOn is between 1 and 7 _and_ is not NaN
2026-07-05 06:41:32 +00:00
if ( ! ( firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7 ) ) {
throw new RangeError ( 'firstWeekContainsDate must be between 1 and 7 inclusively' ) ;
}
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
var firstWeekOfNextYear = new Date ( 0 ) ;
firstWeekOfNextYear . setFullYear ( year + 1 , 0 , firstWeekContainsDate ) ;
firstWeekOfNextYear . setHours ( 0 , 0 , 0 , 0 ) ;
var startOfNextYear = ( 0 , _index . default ) ( firstWeekOfNextYear , options ) ;
var firstWeekOfThisYear = new Date ( 0 ) ;
firstWeekOfThisYear . setFullYear ( year , 0 , firstWeekContainsDate ) ;
firstWeekOfThisYear . setHours ( 0 , 0 , 0 , 0 ) ;
var startOfThisYear = ( 0 , _index . default ) ( firstWeekOfThisYear , options ) ;
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
if ( date . getTime ( ) >= startOfNextYear . getTime ( ) ) {
return year + 1 ;
} else if ( date . getTime ( ) >= startOfThisYear . getTime ( ) ) {
return year ;
} else {
return year - 1 ;
}
}
2026-07-07 07:11:50 +00:00
2026-07-05 06:41:32 +00:00
module . exports = exports . default ;