【问题标题】:Format datepicker not change angularjs格式 datepicker 不改变 angularjs
【发布时间】:2017-11-14 17:57:03
【问题描述】:

我已经为我的日期选择器定义了这样的格式

    $scope.open1 = function() {
    $scope.popup1.opened = true;
  };

  $scope.open2 = function() {
    $scope.popup2.opened = true;
  };
  $scope.formats = ['dd-MMMM-yyyy', 'yyyyMMdd'];
  $scope.format = $scope.formats[1];
  $scope.dt = new Date();
  $scope.dt2 = new Date();
  console.log($scope.dt);
  $scope.popup1 = {
    opened: false
  };

  $scope.popup2 = {
    opened: false
  };

但是当我使用console.log时,结果变成了这样

我想要类似当我 console.log 的结果将与我定义的相同。例如Tue Nov 14 2017 变成20171114

这是我的 .html 文件

<div class="col-md-2">
        <p class="input-group">
          <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt2" is-open="popup2.opened"/>
          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open2()"><i class="glyphicon glyphicon-calendar"></i></button>
          </span>
        </p>
        </div>

【问题讨论】:

  • uib-datepicker-popup="{{formats}}" 应该是 uib-datepicker-popup="{{format}}" 吗?没有's'
  • 我改了,还是一样
  • 我看到你在 new Date() 之后执行了 console.log($scope.dt)。它是你做 console.log 的唯一地方吗?
  • @Charlie Ng ya,因为我想知道 scope.dt 的值
  • 但是$scope.dt是没有格式的,你不想做console.log($scope.format)吗?

标签: javascript angularjs datepicker angular-ui-bootstrap


【解决方案1】:

对我有用,希望对你有帮助

var d = new Date($scope.dt);
  var curr_year = d.getFullYear();
  var curr_Month = d.getMonth()+1;
          if (curr_Month.toString().length < 2) {
            var curr_Month = "0" + (d.getMonth()+1);
          } else {
            var curr_Month = d.getMonth()+1;
          }
          //alert(curr_Month);

          var curr_date = d.getDate();
          if (curr_date.toString().length<2) {
            var curr_date = "0"+ d.getDate();
          } else {
            var curr_date = d.getDate();
          }
          //alert(curr_date);
          var todayDate = (curr_year + "" + (curr_Month) + "" + curr_date);

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    相关资源
    最近更新 更多