使用angularjs和openweathermap将数据从旧数组推送到新数组中进行天气预报

时间:2015-11-10 16:33:10

标签: javascript html arrays angularjs openweathermap

大家好,我一直都在努力让这件事情整个周末都有效,但我似乎无法理解它。我想要实现的是使用openweathermap api从数组中选择weatherdata,然后将此数据推送到新数组中。我的问题是,我想让这项工作像24小时预测,总是在第二天上午06:00开始,因此我只想推送这24小时间隔内存在的数据。但正如我所说,我似乎无法让它发挥作用。

这是我的代码:

HTML

<div ng-app="weatherApp" ng-controller="weatherController">
    <ul>
        <li ng-repeat="x in forecastArray">
            {{x.dt_txt}}
        </li>
    </ul>
</div>

的JavaScript

var app = angular.module('weatherApp', []);

app.controller('weatherController', function($scope, $http) {

    $scope.units = 'metric';
    $scope.apiId = '...'; //My api key  


    $http.jsonp('http://api.openweathermap.org/data/2.5/forecast?', { params : {
        q : "london",
        units : $scope.units,
        callback: 'JSON_CALLBACK',
        APPID: $scope.apiId
    }}).success(function(response){
        $scope.data = response.list;
    });

    $scope.forecastArray = []; //new array
    var dtStart = new Date(...); //Have no clue how to define this object...
    var dtEnd = new Date(+new Date(dtStart) + 86400000); //dtStart + 24 hours

    $scope.data.forEach(function(item){
        var date = new Date(item.dt); //defining the dt property in data as a new dateobj
        if(date > dtStart && date < dtEnd){
            $scope.foreCastArray.push(item);
        }
    });

});

1 个答案:

答案 0 :(得分:1)

所以你基本上会问如何为tommorow早上6点创建一个Date对象。

首先使用

为tommorow创建Date对象
var today = Date();
var tommorow = Date();
tommorow.setDate(today.getDate() + 1);

然后正确设置小时和分钟。 使用Date.setHours(hour,min,sec,millisec)

tommorow.setHours(6,0,0,0);

这应该有效。