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 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化
  • ¥15 如何基于Ryu环境下使用scapy包进行数据包构造
  • ¥15 springboot国际化