Source

dateTime/getWeekOfYear.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MS_IN_DAY = 86400000;
var MAX_WEEKDAY = 7;
var DAY_OFFSET = 4;
/**
 * Based on information at:
 * http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
 *
 * Algorithm is to find nearest thursday, it's year
 * is the year of the week number. Then get weeks
 * between that date and the first day of that year.
 *
 * Note that dates in one year can be weeks of previous
 * or next year, overlap is up to 3 days.
 *
 * e.g. 2014/12/29 is Monday in week 1 of 2015
 *   2012/1/1  is Sunday in week 52 of 2011
 *
 * @author https://stackoverflow.com/a/6117889/9428719
 * @param d
 * @category dateTime
 * @module getWeekOfYear
 */
function getWeekOfYear(date) {
    var d = date;
    // Copy date so don't modify original
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday's day number 7
    d.setUTCDate(d.getUTCDate() + DAY_OFFSET - (d.getUTCDay() || MAX_WEEKDAY));
    // Get first day of year
    var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
    // Calculate full weeks to nearest Thursday
    var weekNum = Math.ceil((((Number(d) - Number(yearStart)) / MS_IN_DAY) + 1) / MAX_WEEKDAY);
    // Return array of year and week number
    return weekNum;
}
exports.default = getWeekOfYear;