繁体   English   中英

在 Javascript / Typescript 中获取给定年、月和周数(相对于月)的星期几

[英]Get Date of days of a week given year, month and week number (relative to month) in Javascript / Typescript

我想要一个function getDaysOfWeekDates ,如果我作为参数传递一年、一个月(0 到 11 之间)和每个月的一周(通常每个月有 4-5 周),它会给我一个包含每一天的日期列表那一周的 例如:

function getDaysOfWeekDates(year: number, month: number, weekNumber: number): Array<Date> {
     //...
}
const days: Date[] = getDaysOfWeekDates(2020, 0, 2) // Second week of January 2020
console.log(days);

/**
 * 2020-01-06T00:00:00.000Z
 * 2020-01-07T00:00:00.000Z
 * 2020-01-08T00:00:00.000Z
 * 2020-01-09T00:00:00.000Z
 * 2020-01-10T00:00:00.000Z
 * 2020-01-11T00:00:00.000Z
 * 2020-01-12T00:00:00.000Z
 */

我能够构建一个像这样工作的 function getDaysOfWeekDates2 ,但是使用一年和一周作为参数(相对于一年的一周,每年有 52-53 周):

function getDaysOfWeekDates2(year: number, weekNumber: number) {
    const [ startDate ] =  getDateRangeOfWeek(year, weekNumber);

    return new Array(7).fill(null).map((e, index) => {
      
      return new Date(
        startDate.getFullYear(), 
        startDate.getMonth(), 
        startDate.getDate() + index
      );
    });
}
function getDateRangeOfWeek(year: number, weekNumber: number){
    
    const date = new Date(String(year));

    const numOfdaysPastSinceLastMonday = date.getDay() - 1;
    date.setDate(date.getDate() - numOfdaysPastSinceLastMonday);
    date.setDate(date.getDate() + (7 * (weekNumber - getWeekNumber(date))));

    const rangeIsFrom =  new Date(date.getFullYear() + "-" +(date.getMonth() + 1) + "-" + date.getDate());
    date.setDate(date.getDate() + 6);
    const rangeIsTo = new Date(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + + date.getDate());
    
    return [rangeIsFrom, rangeIsTo];
};
function getWeekNumber(date: Date): number {
    const dateCopy = new Date(date.getTime());

    dateCopy.setHours(0, 0, 0, 0);
    // Thursday in current week decides the year.
    dateCopy.setDate(dateCopy.getDate() + 3 - (dateCopy.getDay() + 6) % 7);
    // January 4 is always in week 1.
    const week1 = new Date(dateCopy.getFullYear(), 0, 4);
    // Adjust to Thursday in week 1 and count number of weeks from date to week1.
    return 1 + Math.round(((dateCopy.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
  }

知道如何修改getDaysOfWeekDates? 获得所需的 function getDaysOfWeekDates 或者只是使用一些库从头开始构建它

如果您希望星期一作为一周的第一天,并且一个月的第一周是第一个星期四的那一周,那么您可以使用与年份周数 function 类似的算法。

因此,获取所需一周的开始,然后只需循环 7 次即可获取每一天。 例如

 /* Return first day of specified week of month of year ** ** @param {number|string} year - year for required week ** @param {number|string} month - month for required week ** Month is calendar month number, 1 = Jan, 2 = Feb, etc. ** @param {number|string} week - week of month ** First week of month is the one with the first Thursday ** @returns {Date} date for Monday at start of required week */ function getMonthWeek(year, month, week) { // Set date to 4th of month let d = new Date(year, month - 1, 4); // Get day number, set Sunday to 7 let day = d.getDay() || 7; // Set to prior Monday d.setDate(d.getDate() - day + 1); // Set to required week d.setDate(d.getDate() + 7 * (week - 1)); return d; } // Return array of dates for specified week of month of year function getWeekDates(year, month, week) { let d = getMonthWeek(year, month, week); for (var i=0, arr=[]; i<7; i++) { // Array of date strings arr.push(d.toDateString()); // For array of Date objects, replace above with // arr.push(new Date(d)); // Increment date d.setDate(d.getDate() + 1); } return arr; } // Week dates for week 1 of Jan 2020 - week starts in prior year console.log(getWeekDates(2020, 1, 1)); // Week dates for week 5 of Jan 2020 - 5 week month console.log(getWeekDates(2020, 1, 5)); // Week dates for week 1 of Oct 2020 - 1st is a Thursday console.log(getWeekDates(2020, 10, 1)); // Week dates for week 1 of Nov 2020 - 1st is a Sunday console.log(getWeekDates(2020, 11, 1));

目前尚不清楚您是否需要 Date 对象或字符串的数组,因此注释中包含 Date object 版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM