ui.bootstrap.timepicker比它应该少2h

时间:2017-03-29 11:51:52

标签: angularjs twitter-bootstrap

我使用ui.bootstrap.timepicker在我的应用中添加时间间隔。 这是我选择时间的地方,按钮INSERT& SAVE将数据发送到api。 enter image description here

问题是,在这里你可以看到选定的时间,但是当它发送给API时,时间少于2小时。 这是请求有效载荷

{from_time: "2017-03-29T11:37:05.541Z", to_time: "2017-03-29T12:42:05.549Z"}
from_time:"2017-03-29T11:37:05.541Z"
to_time:"2017-03-29T12:42:05.549Z"

这是我的HTML

<form class="form" role="form" ng-submit="addRow(fromUrbanTime, toUrbanTime)">
                    <div class="col-md-12">
                        <div class="col-md-6">
                            <div class="col-md-6">
                                <h5><b>{{'FROM'| translate}} (h)</b></h5>
                                <div uib-timepicker ng-model="fromUrbanTime" hour-step="hstep" minute-step="mstep" show-meridian="ismeridian"></div>
                                    <hr>
                            </div>
                            <div class="col-md-6">
                                <h5><b>{{'TO'| translate}} (h)</b></h5>
                                 <div uib-timepicker ng-model="toUrbanTime" hour-step="hstep" minute-step="mstep" show-meridian="ismeridian"></div>
                                 <hr>
                                <button type="submit" class="btn btn-default">Insert & Save</button>
                            </div>
                        </div>
                    </div>
                     <pre class="alert alert-info">Time is: {{fromUrbanTime | date:'shortTime' }}</pre>
                     <pre class="alert alert-info">Time is: {{toUrbanTime | date:'shortTime' }}</pre>
                </form>

这是我的ctrl

//time picker
$scope.mytime = new Date();

 $scope.hstep = 1;
 $scope.mstep = 5;


 $scope.ismeridian = false;


 $scope.clear = function() {
 $scope.mytime = null;
 };
//adding new time intervals
$scope.addRow = function(fromUrban, toUrban){
var config = {
             headers: {
                "Content-Type": "application/json",
                "X-HTTP-Method-Override": "POST"}
        };
        var data = {
                        "from_time": fromUrban,
                        "to_time": toUrban
                    };

        $http.post(serviceBase + 'aaaaa/' + $scope.urbanConfig.id + '/screen-intervals', data, config)
                .success(function (data, status, headers, config) {
                    Notification.success({message: $filter('translate')('URBAN_TIME_RANGE'), delay: 3000, positionY: 'bottom', positionX: 'right'});
                    //startTimer();
                    $scope.urbanTimeRange.push(data);
                    $scope.$watchCollection('urbanTimeRange', function (newValue, oldValue, scope) {
}, true);
                })

1 个答案:

答案 0 :(得分:1)

您当地的时区似乎是GMT + 2。在JavaScript中创建的Date个对象始终位于本地时区。但是,您的后端似乎在UTC中工作,即GMT + 0,因此在后端进行转换。如果是这种情况,那么当后端将日期发送回客户端时,包括时区信息,它们应该再次在本地时区创建,意味着+2小时。

示例:

var now = new Date()
console.log(now.toString()); // "Wed Mar 29 2017 15:03:10 GMT+0200 (FLE Daylight Time)"

发送到服务器后,转换为ISO格式,应该变成:

"2017-03-29T13:03:10.000Z"

然后如果该值传递给客户:

var now = new Date("2017-03-29T13:03:10.000Z");
console.log(now.toString()); // "Wed Mar 29 2017 15:03:10 GMT+0200 (FLE Daylight Time)"