【问题标题】:Change the date time format in angularjs更改angularjs中的日期时间格式
【发布时间】:2016-09-15 21:04:43
【问题描述】:

我从 api 获取日期和时间。我想更改日期和时间格式。

我越来越喜欢“2016-05-12”,“07:17:35”。

更改格式,例如 2016 年 5 月 12 日晚上 7:30:

<table class="table">
    <thead>
        <tr>
            <th>Time</th>
            <th>Place</th>
        </tr>
    </thead>
    <tbody ng-repeat="l in dataget">
        <tr>                    
            <td>{{l['time']}}</td>
            <td>{{l['place']}}</td>                             
        </tr>
    </tbody>
</table>

【问题讨论】:

  • 你有这样"2016-05-12","07:17:35","2016-05-12 07:17:35"这样的约会吗??
  • 我是这样的 ["2016-05-12","07:17:35"]

标签: javascript jquery angularjs html


【解决方案1】:

使用日期过滤器

<table class="table" ng-repeat="a in list_alerts">
<thead><tr>
<th>Time</th>
<th>Place</th>
</tr>
</thead>
<tbody ng-repeat="l in dataget">
<tr>                    
<td>{{l['time']| date : format}}</td>
<td>{{l['place']}}</td>                             
</tr>
</tbody>
</table>

格式应替换为可能接受的值之一,对于显示所需输出的格式,请检查 angular api 中的可能值: https://docs.angularjs.org/api/ng/filter/date

这是一个使用“短”格式的示例:

<table class="table" ng-repeat="a in list_alerts">
<thead><tr>
<th>Time</th>
<th>Place</th>
</tr>
</thead>
<tbody ng-repeat="l in dataget">
<tr>                    
<td>{{l['time']| date : 'short'}}</td>
<td>{{l['place']}}</td>                             
</tr>
</tbody>
</table>

【讨论】:

    【解决方案2】:

    在带有管道符号的对象之后使用日期:格式。

    For Example Click here

    【讨论】:

      【解决方案3】:

      我认为这有帮助,

      app.controller("myCtrl", function($scope,$filter) {
          $scope.dateString = '"2016-05-12","07:17:35"';
          $scope.finalFormat = $filter('date')(new Date($scope.dateString.substring(1,11) +" "+ $scope.dateString.substring(14,22)),'dd-MMM-yyyy hh:mm a');
      });
      

      【讨论】:

        【解决方案4】:

        正如here 所说,你需要这样写: {{yourDate | date: 'dd-MMM-yyyy hh:mm a'}}
        结果将是12-May-2016 7:30 PM

        【讨论】:

          【解决方案5】:

          检查日期格式https://docs.angularjs.org/api/ng/filter/date 并像这样使用它:

          {{your_date | date:'dd-MMM-yyyy hh:mm a'}}
          

          在这里玩http://jsfiddle.net/vpfxdrum/。 在您的情况下使用它:

          <table class="table">
          <thead>
              <tr>
                  <th>Time</th>
                  <th>Place</th>
              </tr>
          </thead>
          <tbody ng-repeat="l in dataget">
              <tr>                    
                  <td>{{l['time'] | date:'dd-MMM-yyyy hh:mm a'}}</td>
                  <td>{{l['place']}}</td>                             
              </tr>
          </tbody>
          

          【讨论】:

          • 我想从 ng-repeat 改变
          【解决方案6】:

          *controller.js

            var myApp = angular.module('myApp', []);
            myApp.filter('datetime', function($filter)
            {
              return function(input)
               {
                if(input == null){ return ""; } 
                var _date = $filter('date')(new Date(input),
                                    'MMM dd yyyy - HH:mm:ss'); 
               return _date.toUpperCase();
           };
          });
          

          在 HTML 中

          <div ng-app="sampleapp">
          <table class="table">
          <thead>
              <tr>
                  <th>Time</th>
                  <th>Place</th>
              </tr>
          </thead>
          <tbody ng-repeat="l in dataget">
              <tr>                    
                  <td>{{l['time'] | datetime}}</td>
                  <td>{{l['place']}}</td>                             
              </tr>
          </tbody>
          

          【讨论】:

            猜你喜欢
            • 2020-02-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-23
            • 2013-06-29
            • 2017-05-13
            • 2015-08-04
            • 1970-01-01
            相关资源
            最近更新 更多