1

I'm trying to display a date in the format "dd/mm/yyyy" in AngularJS. I'm retrieving data from database with symfony2.

When I inspect my scope in the browser it looks like I have a valid date object:

CreationDate : Object { date="2014-05-15 00:00:00", timezone_type=3, timezone="Europe/Paris"}

Here is what i have tried:

{{res.creationDate  | date:'dd/MM/yyyy' }}

which prints this:

{"date":"2014-08-12 00:00:00","timezone_type":3,"timezone":"Europe/Paris"}

and quite logically:

{{res.creationDate.date }} --> 2014-05-15 00:00:00`

I'd like to have "15-05-2014"

---- EDIT

My controller:

public function ViewAction()
    {
        $repository = $this->getDoctrine()->getManager()->getRepository('AcmeMyBundle:MyEntity');


        $list = $repository->getArray();
        $list = json_encode($list);

        return $this->render('AcmeMyBundle:Search:search.html.twig', array('list' => $list) );
    }

My custom getArray function in repo:

public function getArray()
{
    $query = $this->createQueryBuilder('s');
    return $query->getQuery()->getArrayResult();
}

My template looks something like this:

<div class="col-md-10 col-md-offset-1" ng-init= "result= {{list}}">
    <ul class="list-group">
        <div ng-repeat="res in result | filter:searchText | filter:nom | filter :prenom | limitTo:50 " >

                    <li class="list-group-item">Date {% verbatim %} {{res.creationDate | date:'dd/MM/yyyy' }} {% endverbatim %}</li>
        </div>
    </ul>

creationDate is a "date" format, in a SQL Database. In phpmyadmin it prints 'dd-mm-yyy'

Graham
  • 7,431
  • 18
  • 59
  • 84
oligan
  • 634
  • 6
  • 18

4 Answers4

0

If you try to format the date directly?

{{res.creationDate.date  | date:'dd/MM/yyyy' }}
Flo_1609
  • 33
  • 1
  • 5
  • didn't want to be redundant.. but still prints 2014-05-15 00:00:00. as res.creationDate.date isn't an object ( well that's my guess :p ) – oligan Aug 14 '14 at 09:29
  • Ok. Can you create an object with your date? Like new Date(res.creationDate.data); And then display this object? – Flo_1609 Aug 14 '14 at 10:22
0

If you can alter the date object from date="2014-05-15 00:00:00" to date="2014-05-15T00:00:00" it should work.

Working example at this JSFiddle

You might also want to take a look on this post about JSON date format

Community
  • 1
  • 1
Søren Dam
  • 1,545
  • 1
  • 15
  • 17
0

If anyone wondering.. Here is a trick i came up with for my problem

view

{{ mySplit(res.symfonyDateObject.date, 0) | date:'dd-MM-yyyy' }}

controller

app.controller('SearchController', function($scope)
{

    $scope.mySplit = function(string, nb) {
        $scope.array = string.split(' ');
        return $scope.array[nb];

    }
});

tldr i split the string in half, remove the hh/mm/ss and then angular format works

oligan
  • 634
  • 6
  • 18
0

You can use javascript Date object to convert your timestamp(2014-05-15 00:00:00) into a time string(1400092200000) and apply angular filter

Working example at this JSFiddle

Srihari Goud
  • 824
  • 10
  • 25