1

I wrote a notification app using swampdragon. Unfortunately, pub_date variable includes microseconds which I don't want it to be there. How can I format it like 'dd-MM-yyyy HH:mm:ss' or like in the admin site(a.m p.m version)? My controller.js:

var AnnounceControllers = angular.module('AnnounceControllers', []);


AnnounceControllers.controller('AnnounceListCtrl', ['$scope', '$dragon', function ($scope, $dragon) {
$scope.announceList = {};
$scope.announcements = [];
$scope.channel = 'announce';

$dragon.onReady(function() {
    $dragon.subscribe('announcements', $scope.channel, {announce_list__id: 1}).then(function(response) {
        $scope.dataMapper = new DataMapper(response.data);
    });

    $dragon.getSingle('announce-list', {id:1}).then(function(response) {
        $scope.announceList = response.data;
    });

    $dragon.getList('announcements', {list_id:1}).then(function(response) {
        $scope.announcements = response.data;
    });
});

$dragon.onChannelMessage(function(channels, message) {
    if (indexOf.call(channels, $scope.channel) > -1) {
        $scope.$apply(function() {
            $scope.dataMapper.mapData($scope.announcements, message);
        });
    }
});
}]);

My app.js:

var AnnounceApp = angular.module('AnnounceApp', [
'SwampDragonServices',
'AnnounceControllers'
]);

In HTML :

<h3 ng-repeat="item in announcements">
        <div class="alert alert-info" role="alert">
          {{ item.content }} {{item.pub_date| date:'dd-MM-yyyy HH:mm:ss'}}
          <br>
        </div>
      </h3>

models.py:

pub_date = models.DateTimeField(auto_now_add = True)

serializers.py:

class AnnounceSerializer(ModelSerializer):
class Meta:
    model = "post.Announce"
    publish_fields = ("content","pub_date")
Daniel Cottone
  • 4,257
  • 24
  • 39
kings856
  • 73
  • 10
  • If the angular date filter is not working, it could be that `pub_date` is a string, try parsing it to a JavaScript date? – Starscream1984 Jul 15 '15 at 12:46
  • I added serializers.py to the question also I tried new models.TextField and it is actually use pre_save signal to transform pub_date to isoformat but it did not work – kings856 Jul 20 '15 at 14:11

2 Answers2

1

You have to convert the DateTimeField to the ISO 8061 format so that the AngularJS date filter will work:

pub_date = models.DateTimeField(auto_now_add = True).isoformat(' ')

See this for more info.

EDIT:

After further clarification, the problem is that the timestamp you are getting is not formatted properly for javascript to recognize it as a date. See this plunkr for to how fix that.

Daniel Cottone
  • 4,257
  • 24
  • 39
  • I am using swampdragon tutorial and in this tutorial to serialize objects ModelSerializer is used and I know that "iso-8601" is the output format. I am seeing the output as 2015-07-20 16:52:51.347082 – kings856 Jul 20 '15 at 14:05
  • Ah, ok I see your problem now. You have to parse the data you are getting into a proper timestamp, right now its just a string. [See this plunkr.](http://plnkr.co/edit/XFCZEidrEpyVvMC2QIWA) – Daniel Cottone Jul 20 '15 at 14:23
  • is it really working because when ı run this plunker output date is null ? – kings856 Jul 20 '15 at 16:11
  • [Yeah, here's a screenshot.](http://i.imgur.com/VYjOCdi.png) I'm not sure why it wouldn't be working for you. – Daniel Cottone Jul 20 '15 at 16:14
  • I tried both Windows and Lubuntu and date is null not changed. Can ı do that with different method ? – kings856 Jul 20 '15 at 16:32
  • It looks like parsing a date in that format only works in Chrome. You have 2 options then. You can either use a different [library to parse the date](http://www.datejs.com/) or you can return the date in a valid javascript format. – Daniel Cottone Jul 20 '15 at 16:42
  • How can ı return the date in a valid javascript format ? – kings856 Jul 20 '15 at 17:17
  • It must conform to the [ISO-8601 format](http://www.w3.org/TR/NOTE-datetime). You can also [read this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) for more information. – Daniel Cottone Jul 20 '15 at 17:23
  • I have one more question about date how can I change the language of this date how to be done ınternalization ? – kings856 Jul 22 '15 at 06:20
0

I used some info from Daniel Cottone and did those:

class Announce(SelfPublishModel, models.Model):
serializer_class = AnnounceSerializer
announce_list = models.ForeignKey(AnnounceList)
content = models.CharField(max_length=100)
pub_date = models.DateTimeField(default=datetime.now)
date = models.TextField(null=True)

def __unicode__(self):
    return self.content


@receiver(pre_save,sender=Announce)
   def date_handler(sender,instance,*args,**kwargs):
       instance.date = instance.pub_date.isoformat()

isoformat without parameters worked for me.

kings856
  • 73
  • 10