简体   繁体   English

如何从当前日期的数组中获得7个连续日期(数组中的日期不同)?

[英]How do i get 7 consecutive dates(dates are different in array) from an array onwards the current date?

Lets say my current date is 'May 9' , now i wish to get 7 consecutives events(dates) after 'May 9'. 假设我现在的日期是'5月9日',现在我希望在'5月9日'之后连续7次举办活动(日期)。 Here's my json array sample 这是我的json数组样本

[{
    "date": "4 Mar",
    "day": "Mon",
    "holiday_name": "bbb"
},
{
    "date": "7 Mar",
    "day": "Thu",
    "holiday_name": "ccc"
},
{
    "date": "8 Mar",
    "day": "Fri",
    "holiday_name": "ddd"
},
{
    "date": "5 Apr",
    "day": "Fri",
    "holiday_name": "eee"
},
{
    "date": "14 Apr",
    "day": "Sun",
    "holiday_name": "fff"
},
{
    "date": "14 Apr",
    "day": "Sun",
    "holiday_name": "gfgg"
},
{
    "date": "24 Apr",
    "day": "Wed",
    "holiday_name": "ttt"
}, ...
//more in the list]

I get the current date with this, 我得到了这个当前日期,

let day = date.getDate(); const monthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; let month = monthNames[date.getMonth()]; let appendDate = day + " " + month;

First you can sort the array in ascending order of date. 首先,您可以按日期的升序对数组进行排序。 Then use filter and get a new array where date is greater or equal to required date. 然后使用filter并获取一个新数组,其中date大于或等于所需日期。 In your case it is May 9 & in the example below I have considered today's date. 在你的情况下,它是May 9 ,在下面的例子中,我考虑了今天的日期。 After that you can use slice to get required number of days. 之后,您可以使用slice来获取所需的天数。 In your case it is 9 but for example I have considered 2 在你的情况下它是9但是例如我考虑了2

 let date = [{ "date": "3 Mar", "day": "Mon", "holiday_name": "bbb" }, { "date": "4 Mar", "day": "Mon", "holiday_name": "bbb" }, { "date": "7 Mar", "day": "Thu", "holiday_name": "ccc" }, { "date": "8 Mar", "day": "Fri", "holiday_name": "ddd" }, { "date": "5 Apr", "day": "Fri", "holiday_name": "eee" }, { "date": "14 Apr", "day": "Sun", "holiday_name": "fff" }, { "date": "14 Apr", "day": "Sun", "holiday_name": "gfgg" }, { "date": "24 Apr", "day": "Wed", "holiday_name": "ttt" } ]; let dt = date.sort(function(a, b) { return new Date(a.date) - new Date(b.date) }); function getConsDate(dat) { return dt.filter(function(item) { return new Date(item.date) >= new Date(dat) }).slice(0, 2) } console.log(getConsDate('7 March')) 

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

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