1

I'm getting date and time from the api. I want to change the date and time format.

I'm getting like this "2016-05-12","07:17:35".

Change the format like 12-may-2016 7:30 PM:

<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>
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Divakar
  • 31
  • 2
  • 6

6 Answers6

1

use the date filter

<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>

the format should be replaced with one of the possible accepted values, for the format that displays the desired output, check the possible values from angular api: https://docs.angularjs.org/api/ng/filter/date

here is an example that uses the 'short' format:

<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>
Shadi Shaaban
  • 1,670
  • 1
  • 10
  • 17
0

As stated here, you need to write it like this: {{yourDate | date: 'dd-MMM-yyyy hh:mm a'}}.
The result of this will be 12-May-2016 7:30 PM

Marin Takanov
  • 1,079
  • 3
  • 19
  • 36
0

Use date:format after object with pipe symbol.

For Example Click here

Thangadurai
  • 524
  • 1
  • 9
  • 20
0

I think this helps,

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');
});
Venu prasad H S
  • 231
  • 1
  • 8
0

Check date format https://docs.angularjs.org/api/ng/filter/date and use it like this:

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

Play with it here http://jsfiddle.net/vpfxdrum/. Using it in your case:

<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>

Łukasz
  • 2,131
  • 1
  • 13
  • 28
0

*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();
 };
});

In 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>

Divakar
  • 31
  • 2
  • 6