角度:如何在"dd/MM/yyyy"日期对象中隐藏日期字符串



我需要以dd/MM/yyyy格式捕获日期数据,并需要将其作为日期对象发送到API。我正在使用以下指令:

angular.module('tmyapp').directive('formattedDate', function (dateFilter) {
    return {
        require: 'ngModel',
        scope: {
            format: "="
        },
        link: function (scope, element, attrs, ngModelController) {
            ngModelController.$parsers.push(function (data) {
                //convert data from view format to model format
                return dateFilter(data, scope.format); //converted
            });
            ngModelController.$formatters.push(function (data) {
                //convert data from model format to view format
                return dateFilter(data, scope.format); //converted
            });
        }
    }
});
//use <input type="text" formatted-date format="'dd/MM/yyyy'" />

它正在向dd/MM/yyyy显示日期对象,但无法将dd/MM/yyyy格式的数据转换为日期对象。

有人可以帮忙吗?

你必须像这样在解析器中解析日期字符串:

angular.module('tmyapp').directive('formattedDate', function (dateFilter) {
    return {
        require: 'ngModel',
        scope: {
            format: "="
        },
        link: function (scope, element, attrs, ngModelController) {
            ngModelController.$parsers.push(function (data) {
                //convert data from view format to model format
                dateArray = data.split("/");
                var date = new Date(dateArray[2] + "-" + dateArray[1] + "-" + dateArray[0])
                return date; //converted
            });
            ngModelController.$formatters.push(function (data) {
                //convert data from model format to view format
                return dateFilter(data, scope.format); //converted
            });
        }
    }
});

相关内容

  • 没有找到相关文章

最新更新