1

我使用 swampdragon 编写了一个通知应用程序。不幸的是, pub_date 变量包括我不希望它存在的微秒。如何在管理站点(上午版本)中将其格式化为 'dd-MM-yyyy HH:mm:ss' 或类似的格式?我的控制器.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);
        });
    }
});
}]);

我的 app.js:

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

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

模型.py:

pub_date = models.DateTimeField(auto_now_add = True)

序列化程序.py:

class AnnounceSerializer(ModelSerializer):
class Meta:
    model = "post.Announce"
    publish_fields = ("content","pub_date")
4

2 回答 2

1

您必须将 DateTimeField 转换为 ISO 8061 格式,这样 AngularJS 日期过滤器才能工作:

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

有关更多信息,请参阅此内容。

编辑:

经过进一步澄清,问题是您获得的时间戳没有正确格式化,以便 javascript 将其识别为日期。请参阅此 plunkr 以了解如何解决该问题。

于 2015-07-15T12:48:41.127 回答
0

我使用了 Daniel Cottone 的一些信息并做了这些:

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 对我有用。

于 2015-07-22T06:18:22.730 回答