dqkyz02602 2015-07-09 03:42
浏览 66
已采纳

如何将Angular UI datepicker中的ISO格式值更改为yyyy-mm-dd?

I have a problem in my code. I have a datepicker in my form and in the textbox value the date format is correct but when passing in the PHP it is in ISO format just like this:

Sat Aug 29 2015 00:00:00 GMT+0800 (Malay Peninsula Standard Time)

Here's what i have in my sample code:

 <div class="container" ng-app="dateApp">

      <div ng-controller="dateCtrl">
        <h3>TEST DATE</h3>
        <div class="form-group">
          <label>Choose date: </label>
          <p class="input-group">
            <input type="text" class="form-control" csDateToIso datepicker-popup="yyyy-MM-dd" is-open="opened" ng-model="chosenDate" />
            <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
            </span>
          </p>
        </div>

        <div class="well" ng-bind="chosenDate"></div>

      </div>

    </div>

JS:

var dateApp = angular.module('dateApp', ['ui.bootstrap']);

dateApp.controller('dateCtrl', function($scope) {

  $scope.today = function() {
    chosenDate = new Date();
  };

  $scope.today();

  $scope.toggleMin = function() {
    $scope.minDate = $scope.minDate ? null : new Date();
  };

    $scope.toggleMin();

  $scope.clear = function () {
    $scope.dt = null;
  };

  $scope.open = function($event) {
    $event.preventDefault();
    $event.stopPropagation();

    $scope.opened = true;
  };


});

//directive ISO from -entre- not working
dateApp.directive('csDateToIso', function () {

        var linkFunction = function (scope, element, attrs, dateCtrl) {

            dateCtrl.$parsers.push(function (datepickerValue) {
                return moments(datepickerValue).format('YYYY-MM-DD');
            });

        };

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

    });

I want to have a format like this: yyyy-mm-dd hh:i:ss

Here's the plnkr: http://plnkr.co/edit/NNqZ8v?p=preview

  • 写回答

1条回答 默认 最新

  • dongtiao2066 2015-07-09 03:53
    关注

    I had written custom directive which can be used with ng-model and does fixes the date value to be in ISO format and strips off its time part, to get away with problems of timezone.

    csapp.directive('csDateToIso', function () {
    
        var linkFunction = function (scope, element, attrs, ngModelCtrl) {
    
            ngModelCtrl.$parsers.push(function (datepickerValue) {
                return moment(datepickerValue).format('YYYY-MM-DD');
            });
        };
    
        return {
            restrict: 'A',
            require: 'ngModel',
            link: linkFunction
        };
    });
    

    use this directive with ui-datepicker to fix the issue. I am using moment.js, but you can do it without moment.js as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化