简体   繁体   English

使用日期名称和日期以数组形式获取一个月中的周数

[英]Get weeks in a month as array with Day Name and Date

I have a code that return weeks start and end date in a month but I need it return full week with date ,day, month我有一个代码可以在一个月内返回周的开始日期和结束日期,但我需要它返回整周的日期,日,月

month start from 0-11月份从 0-11 开始

function getFullWeeksStartAndEndInMonth (month, year) {
    let weeks = [],
        firstDate = new Date(year, month, 1),
        lastDate = new Date(year, month + 1, 0),
        numDays = lastDate.getDate()

    let start = 1
    let end
    if (firstDate.getDay() === 1) {
        end = 7
    } else if (firstDate.getDay() === 0) {
        let preMonthEndDay = new Date(year, month, 0)
        start = preMonthEndDay.getDate() - 6 + 1
        end = 1
    } else {
        let preMonthEndDay = new Date(year, month, 0)
        start = preMonthEndDay.getDate() + 1 - firstDate.getDay() + 1
        end = 7 - firstDate.getDay() + 1
        weeks.push({
            start: start,
            end: end
        })
        start = end + 1
        end = end + 7
    }
    while (start <= numDays) {
        weeks.push({
            start: start,
            end: end
        });
        start = end + 1;
        end = end + 7;
        end = start === 1 && end === 8 ? 1 : end;
        if (end > numDays && start <= numDays) {
            end = end - numDays
            weeks.push({
                start: start,
                end: end
            })
            break
        }
    }
    return weeks
}

How to get full week date + day + month not just start and end date like this如何获得完整的星期日期+日期+月份,而不仅仅是这样的开始和结束日期

0: {date:30,month:"May",day:"Monday"},{date:31,month:"May",day:"Tuesday"},{date:1,month:"June",day:"Wednesday"},...

Here you go干得好

If you need anything explained let me know, it's just a simple .map that outputs seven objects for the week with the required info如果您需要任何解释,请告诉我,这只是一个简单的 .map,它会在一周内输出七个包含所需信息的对象

at the moment, it's a nested array - each item in the outer array is an array for a week目前,它是一个嵌套数组 - 外部数组中的每个项目都是一个一周的数组

if you want to flatten it, uncomment the .flat(Infinity)如果你想展平它,取消注释.flat(Infinity)

 function getFullWeeksStartAndEndInMonth (month, year) { let weeks = [], firstDate = new Date(year, month, 1), lastDate = new Date(year, month + 1, 0), numDays = lastDate.getDate() let start = 1 let end if (firstDate.getDay() === 1) { end = 7 } else if (firstDate.getDay() === 0) { let preMonthEndDay = new Date(year, month, 0) start = preMonthEndDay.getDate() - 6 + 1 end = 1 } else { let preMonthEndDay = new Date(year, month, 0) start = preMonthEndDay.getDate() + 1 - firstDate.getDay() + 1 end = 7 - firstDate.getDay() + 1 weeks.push({ start: start, end: end }) start = end + 1 end = end + 7 } while (start <= numDays) { weeks.push({ start: start, end: end }); start = end + 1; end = end + 7; end = start === 1 && end === 8 ? 1 : end; if (end > numDays && start <= numDays) { end = end - numDays weeks.push({ start: start, end: end }) break } } // *** the magic starts here return weeks.map(({start, end}, index) => { const sub = +(start > end && index === 0); return Array.from({length: 7}, (_, index) => { const date = new Date(year, month - sub, start + index); return { date: date.getDate(), month: date.toLocaleString('en', {month: 'long'}), day: date.toLocaleString('en', {weekday: 'long'}), }; }); })//.flat(Infinity); } console.log(getFullWeeksStartAndEndInMonth(6, 2022))

The output example in OP: OP中的输出示例:

0: {date: 30, month: "May", day: "Monday"},
   {date: 31, month: "May", day: "Tuesday"},
   {date: 1, month: "June", day: "Wednesday"},...

The 0: implies that everything to the right of it is first element of an array. 0:意味着它右边的所有东西都是数组的第一个元素。 If so then it needs brackets [] , or it's a string which needs quotes "".如果是这样,那么它需要括号[] ,或者它是一个需要引号“”的字符串。 In the OP the result looks like an array with another array that has objects.在 OP 中,结果看起来像一个数组,其中包含另一个包含对象的数组。 The outer array is pointless.外部数组是没有意义的。 The example provided returns an array of objects, which is one of the most commonly used data-structures:提供的示例返回一个对象数组,这是最常用的数据结构之一:

[
  {date: 26, month: "May", day: "Thursday"},
  {date: 27, month: "May", day: "Friday"},
  {date: 28, month: "May", day: "Saturday"},
  {date: 29, month: "May", day: "Sunday"},...
]

Details are commented in example细节在例子中注释

 /** * @desc Given a start string date and an end string date, return * each day as an object consisting of {date, month, weekday} * in an array * @param {string<date>} from - A string in this format yyyy-mm-dd * @param {string<date>} to - See previous * @return {array<object>} An array of objects with the following: * {date: {number}, month: {string}, day: {string}} */ function dateRange(from, to) { // Convert the parameters into Date objects const start = new Date(from) const end = new Date(to); // Define array to return let range = []; /* While the start date is less than or equal to the end date... ...get a new date object setting it's date number ahead by 1... ...next get the new date... ...then the long version of the month... ...and then the long version of the weekday... ...create an object with those values matched to these keys: date, month, day... ...add the object to the range array */ while(start <= end) { const next = new Date(start.setDate(start.getDate()+1)); const date = next.getDate(); const month = next.toLocaleString('default', {month: 'long'}); const day = next.toLocaleString('default', {weekday: 'long'}); const dayObj = Object.assign({}, {date: date, month: month, day: day}); range.push(dayObj); } // After the while loop, return the range array return range; } console.log(dateRange('2022-05-26', '2022-06-12'));

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

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