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 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置