Datepicker将时间重置为UTC

时间:2016-02-19 15:55:32

标签: jquery angularjs datepicker momentjs utc

我有一个简单的自定义Angular DatePickerDirective。 问题是 - 当我点击它然后输出时,值被转换为UTC。 如何防止?

import moment = require('moment');

class DatePickerDirective implements ng.IDirective {
    constructor() {
        return {
            restrict: 'A',
            require: '?ngModel',
            link: ($scope: any, $element: any, attrs: any, ngModel: any): void => {

                var defaultOptions: any = {
                    format: 'dd.mm.yyyy',
                    autoclose: true
                };

                $element.datepicker(angular.extend({}, defaultOptions, $scope.options));

                ngModel.$formatters.unshift((val: any) => {
                    var date: Date = moment(val).toDate();
                    var picker: any = $element.data('datepicker');
                    picker.dates.replace([date]);
                    picker.viewDate = date;
                    picker.fill();
                    return date;
                });

                ngModel.$parsers.unshift((val: any) => {
                    return moment(val);
                });
            }
        };
    }
}
export = DatePickerDirective;

2 个答案:

答案 0 :(得分:0)

试试这个指令



 app.directive('datepickerLocaldate', ['$parse', function ($parse) {
        var directive = {
            restrict: 'A',
            require: ['ngModel'],
            link: link
        };
        return directive;

        function link(scope, element, attr, ctrls) {
            var ngModelController = ctrls[0];

            // called with a JavaScript Date object when picked from the datepicker
            ngModelController.$parsers.push(function (viewValue) {
                // undo the timezone adjustment we did during the formatting
                viewValue.setMinutes(viewValue.getMinutes() - viewValue.getTimezoneOffset());
                // we just want a local date in ISO format
                return viewValue.toISOString().substring(0, 10);
            });

            // called with a 'yyyy-mm-dd' string to format
            ngModelController.$formatters.push(function (modelValue) {
                if (!modelValue) {
                    return undefined;
                }
                // date constructor will apply timezone deviations from UTC (i.e. if locale is behind UTC 'dt' will be one day behind)
                var dt = new Date(modelValue);
                // 'undo' the timezone offset again (so we end up on the original date again)
                dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset());
                return dt;
            });
        }
    }]);




答案 1 :(得分:0)

我用它来从UTC转换为ISO。只需将date-to-iso属性添加到输入元素即可。

 myApp.directive("dateToIso", DateToIso);
 function DateToIso () {
    var linkFunction = function (scope, element, attrs, ngModelCtrl) {
        ngModelCtrl.$parsers.push(function (datepickerValue) {
           var dt = new Date(datepickerValue);
           dt.setMinutes(dt.getMinutes() + dt.getTimezoneOffset());
           return dt;
        });
    };

    return {
        restrict: "A",
        require: "ngModel",
        link: linkFunction
    };
}