如何使用Javascript打破开始日期和结束日期之间的数据

时间:2018-01-10 12:14:33

标签: javascript date

我需要一个帮助。我需要使用Javascript按start date and end date打破数据。我在下面解释我的代码。

var data = {
    eid: "9",
    end_date: "2018-01-14",
    event: true,
    mname: "test event2",
    start_date: "2018-01-12",
    user_type: "1"
}

我在这个数组中有start_date =2018-01-12" and end_date:"2018-01-14"。我需要从start date and end date获取每个日期的所有数据,并在下面给出expected output

var data = [{
    eid: "9",
    date: "2018-01-12",
    event: true,
    mname: "test event2",
    user_type: "1"
}, {
    eid: "9",
    date: "2018-01-13",
    event: true,
    mname: "test event2",
    user_type: "1"
}, {
    eid: "9",
    date: "2018-01-14",
    event: true,
    mname: "test event2",
    user_type: "1"
}]

我的输出应该如上所述。

3 个答案:

答案 0 :(得分:2)

使用start_dateend_date创建日期对象,并使用while循环从开始日期到结束日期进行迭代,并继续在数组中添加对象。

var data = {
    eid: "9",
    end_date: "2018-01-14",
    event: true,
    mname: "test event2",
    start_date: "2018-01-12",
    user_type: "1"
},
    endDate = new Date(data.end_date),
    startDate = new Date(data.start_date),
    result = [];
    
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}
    
while(endDate >= startDate) {
  var {eid, event, mname, user_type} = data;
  result.push({eid, event, mname, user_type, date: formatDate(startDate)});
  startDate.setDate(startDate.getDate() + 1);
}
console.log(result);

答案 1 :(得分:0)

只需使用日期start_dateend_date进行比较。

var obj = {
    eid: "9",
    end_date: "2018-01-14",
    event: true,
    mname: "test event2",
    start_date: "2018-01-12",
    user_type: "1"
}

var data = [{
    eid: "9",
    date: "2018-01-12",
    event: true,
    mname: "test event2",
    user_type: "1"
}, {
    eid: "9",
    date: "2018-01-13",
    event: true,
    mname: "test event2",
    user_type: "1"
}, {
    eid: "9",
    date: "2018-01-14",
    event: true,
    mname: "test event2",
    user_type: "1"
},
{
    eid: "9",
    date: "2018-01-16",
    event: true,
    mname: "test event2",
    user_type: "1"
}]

var newdata = data.filter((item) => {
return new Date(item.date) > new Date(obj.start_date) && new Date(item.date) < new Date(obj.end_date)
})
console.log(newdata);

答案 2 :(得分:0)

试试这个

var InputData = {
    eid: "9",
    end_date: "2018-01-14",
    event: true,
    mname: "test event2",
    start_date: "2018-01-12",
    user_type: "1"
}

var StartDate=moment(InputData.start_date, "YYYY-MM-DD");
var EndDate=moment(InputData.end_date, "YYYY-MM-DD");
var Days=EndDate.diff(StartDate, 'days')

var OutputData=[];

var firstObj={
    eid: InputData.eid,
    date: moment(StartDate).format("YYYY-MM-DD"),
    event:InputData.event ,
    mname:InputData.mname,
    user_type: InputData.user_type
}

OutputData.push(firstObj);

for(var i=1;i<Days+1;i++){
   var mydate=moment(StartDate).add(i, 'day').format("YYYY-MM-DD");
   
   var myObj={
    eid: InputData.eid,
    date: mydate,
    event:InputData.event ,
    mname:InputData.mname,
    user_type: InputData.user_type
   };
   
   OutputData.push(myObj);
}

console.log(OutputData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>