基于json数据的日期计算

时间:2018-06-22 12:20:51

标签: javascript arrays json momentjs javascript-objects

我有JSON旅行数据,如果旅行是在国家之间进行的,则将旅行分为具有出发城市,出发国家/地区,出发日期,到达城市,到达国家/地区,到达日期和过境日期的部分。

"data": [
        {
        "depCity": "Warszawa",
        "depCountry": "Poland",
        "depDate": "2018-05-26 13:00",
        "arrCity": "Berlin",
        "arrCountry": "Germany",
        "arrDate": "2018-05-26 18:00",
        "borderDate": "2018-05-26 16:00"
        },
        {
        "depCity": "Berlin",
        "depCountry": "Germany",
        "depDate": "2018-05-27 10:00",
        "arrCity": "Copenhagen",
        "arrCountry": "Denmark",
        "arrDate": "2018-05-27 17:00",
        "borderDate": "2018-05-27 14:00"
        },
        {
        "depCity": "Copenhagen",
        "depCountry": "Denmark",
        "depDate": "2018-05-27 18:00",
        "arrCity": "Berlin",
        "arrCountry": "Germany",
        "arrDate": "2018-05-27 23:00",
        "borderDate": "2018-05-27 20:00"
        },
        {
        "depCity": "Berlin",
        "depCountry": "Germany",
        "depDate": "2018-05-28 14:00",
        "arrCity": "Warszawa",
        "arrCountry": "Poland",
        "arrDate": "2018-05-28 18:00",
        "borderDate": "2018-05-28 16:00"
        }
    ]
}

我正在尝试实现一个JavaScript对象,该对象列出了旅行期间的日期以及在该特定日期访问的国家/地区以及在这些特定国家/地区花费的时间。除第一和最后一天外,每天的时间总和应等于24小时。所以它应该像这样:

"days": [
    {
        "date": "2018-05-26",
        "periods": [
            {
            "country": "A", 
            "time": "02:00"
            },
            {
            "country": "B", 
            "time": "21:00"
            }
        ]
    },
        {
        "date": "2018-05-27",
        "periods": [
            {
            "country": "A", 
            "time": "10:00"
            },
            {
            "country": "B", 
            "time": "14:00"
            }
        ]
    },
        {
        "date": "2018-05-28",
        "periods": [
            {
            "country": "A", 
            "time": "02:00"
            },
            {
            "country": "B", 
            "time": "20:20"
            },
            {
            "country": "C",
            "time": "01:40"
            }
        ]
    }
]

我正在使用MomentJS和jQuery。在下面可以找到我的代码。我所做的是创建了一个对象,并将其每天添加到其中。对象的国家/时间周期数组。但是我不知道如何使每天出现的独特国家/地区以及如何计算每个国家/地区所花费的时间,但要记住旅行之间也存在时间,因此也需要将其包括在内。用户可以在一天之内多次在国家之间旅行。您能否建议如何获得这些国家以及如何计算每个国家/地区的时间花费?

var days = [];
var firstDay = moment(data[0].depDate);
var lastDay = moment(data[data.length-1].arrDate);
i = firstDay;

while (i <= lastDay) {
    var day = {};
    var day.periods = [];
    var period = {};
    var day.date = i.format("YYYY-MM-DD");


    $.each(data, function(j) {
        if (i.isBetween(moment(data[j].depDate), moment(data[j].arrDate), 'day', '[]') ) {
            period.country = data[j].depName;
            period.time = "Calculated Time";
            day.periods.push(period);
            period = {};

            period.country = data[j].arrName;
            period.time = "Calculated Time";
            day.periods.push(period);
            period = {};
        } else if ( j < data.length -1) {
            if ( i.isBetween(moment(data[j].arrDate), moment(data[j+1].depDate), 'day', '()') ) {
                period.country = data[j].arrName;
                period.time = "Calculated Time";
                day.periods.push(period);
                period = {};
            }
        }

    });


    days.push(day);
    i = i.clone().add(1, 'd');
}

0 个答案:

没有答案