【问题标题】:Not expected date format - Difference between ng and date input type不是预期的日期格式 - ng 和日期输入类型之间的区别
【发布时间】:2016-06-18 03:09:42
【问题描述】:

我在 HTML5 页面中有一个带有一些日期类型输入的表单,我想通过 Angular 获取值。如screenshot 所示,它采用非预期格式。输入类型是日期而不是日期时间。

我测试发送此表格,我收到了正确的预期日期,所以 问题是如何在 angulare 中获得正确的日期?

感谢您的帮助!

代码如下:

<fieldset>

               <div class="form-group">
                    <label for="start_date" class="col-sm-2 control-label">Date de début</label>
                    <div class="col-sm-10">
                       <input type="date" name="start_date" ng-model="start_date" class="form-control" required="required">
                    </div>
              </div> 

              <div class="form-group">
                    <label for="end_date" class="col-sm-2 control-label">Date de fin</label>
                    <div class="col-sm-10">
                       <input type="date" name="end_date" ng-model="end_date" class="form-control" required="required">
                    </div>
              </div>

              <div class="form-group">
                 <label for="input" class="col-sm-2 control-label">Type de contact:</label>
                 <div class="col-sm-2">
                    <select ng-model="type" id="input" class="form-control" required="required">
                       <option value="all">Tous</option>
                       <option value="fiche">Demande de renseignements ( DR )</option>
                       <option value="telephone">Demande de rappel</option>
                       <option value="contact">Général</option>
                    </select>
                 </div>
              </div>

              <div class="form-group">
                 <div class="col-sm-10 col-offset-2">
                    <button type="submit"  ng-click="getcontacts(start_date, end_date, type);" class="btn btn-primary">Lancer la recherche</button>
                 </div>
              </div>


              {{ start_date}} {{end_date}} {{ type }}

【问题讨论】:

  • 您可以使用 moment.js 来格式化您的日期,这是一个非常强大的解决方案
  • 不清楚是什么问题。

标签: angularjs html forms


【解决方案1】:

您的输入是“日期”类型,因此您的模型是日期类型。如果要获取日期的字符串表示形式,可以使用 $filter 服务。像这样的:

https://jsfiddle.net/relferreira/y7y5n62c/2/

angular.module('app', []);

angular.module('app')
    .controller('MainController', mainController);

mainController.$inject = ['$filter'];

function mainController($filter){

    var vm = this;

    vm.date = new Date();
    vm.formatedDate = '';
    vm.selectDate = selectDate;

  function selectDate(){
    vm.formatedDate = $filter('date')(vm.date);
  }

}

HTML:

<div data-ng-app="app">

  <div data-ng-controller="MainController as mainVm">
    <input type="date" data-ng-model="mainVm.date" />
    <button data-ng-click="mainVm.selectDate()">Select</button>
    {{mainVm.formatedDate}}
  </div>

</div>

【讨论】:

  • 感谢您的回答。为了避免必须单击所选按钮,有没有办法在我现有的控制器中转换为格式?
【解决方案2】:

感谢Renan,发现我的问题终于和AngularJS - convert dates in controller一样了

注入 $filter 后,我在 Controller 中进行了以下转换

它是一种魅力,再次感谢!

icademieApp.controller( 'contactsCtrl', ['$scope', '$filter', 'contactService',  function ($scope, $filter, contactService) {   

$scope.getcontacts = function ( start_date, end_date, type ){           

        console.log( start_date + ' & ' +  end_date  );

        start_date  = $filter('date')(start_date, "yyyy-MM-dd");
        end_date  = $filter('date')(end_date, "yyyy-MM-dd");

        console.log( start_date + ' & ' +  end_date  ); 

        ....        
}

}]);

第一个日志:

2016 年 3 月 1 日星期二 00:00:00 GMT+0100(巴黎,马德里)和 2016 年 3 月 4 日星期五 00:00:00 GMT+0100(巴黎,马德里)

第二个日志:

2016-03-01 & 2016-03-04

【讨论】:

  • 很好,它有帮助!如果您愿意,请将其标记为正确答案!
猜你喜欢
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2012-06-03
  • 2020-10-19
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多