数据库插入Bootstrap datepicker& timepicker值错了

时间:2015-04-17 11:20:05

标签: mysql angularjs twitter-bootstrap angular-ui-bootstrap bootstrap-datepicker

我在我的应用程序中使用Bootstrap datepicker让用户选择日期。

当我测试它并选择30th Apr时,我得到的值是Thu Apr 30 2015 00:00:00 GMT+0200 (Rom, sommertid) - (我住在丹麦,如果有帮助的话)。我的问题是我的数据库中保存的值是2015-04-29 22:00:00,这是实际选择日期的前一天。

我尝试通过创建服务来解决问题,并在将日期插入数据库之前对其进行格式化。

服务

app.service('formatDateService', ['$q', function ($q) {
    this.formatDateTime = function (date) {
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();

        if (month.toString().length == 1) {
            var month = '0' + month;
        }

        if (day.toString().length == 1) {
            var day = '0' + day;
        }

        if (hour.toString().length == 1) {
            var hour = '0' + hour;
        }

        if (minute.toString().length == 1) {
            var minute = '0' + minute;
        }

        if (second.toString().length == 1) {
            var second = '0' + second;
        }

        var dateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
        return dateTime;
    };
}]);

控制器

app.controller('CourseCreateController', ['$http', '$scope','$sessionStorage','$log','Session','api','divisionService','competenceService', 'formatDateService','$filter',  function ($http, $scope, $sessionStorage,$log, Session, api, divisionService, competenceService, formatDateService, $filter) {
    $scope.createCourse = function () {
        $scope.course.assignment_end = formatDateService.formatDateTime($scope.course.assignment_end);
        $scope.course.date = formatDateService.formatDateTime($scope.course.date);
        $scope.course.start_time = formatDateService.formatDateTime($scope.course.start_time);
        $scope.course.end_time = formatDateService.formatDateTime($scope.course.end_time);

        $http({url: api.getUrl('course', null),
            method: "POST",
            data: {
                course: $scope.course
            }
        }).success(function (data, status, headers, config) {
        }).error(function (data, status, headers, config) {
        });
    };

我和Bootstrap timepicker有同样的问题,可以追溯到2小时。

我觉得它与时区有关,但我真的不知道如何修复它。提前谢谢!

0 个答案:

没有答案