2012-06-26 36 views
0

我在我的django模型中有models.DateTimeField()字段。django tastypie和backbone的日期

我正在使用骨干来显示此模型。我希望能够漂亮地打印日期。

我尝试以下,但它说无效日期:

App.House = Backbone.Model.extend({ 
    url: function() { 
     return API_URL + this.id; 
    }, 
    initialize: function() { 
     // displays Invalid Date when printed in template 
     this.set('pretty_created_at',new Date(this.get('created_at'))); 
    }, 
}); 

当从服务器API响应看,我看到日期的格式如下:

"created_at": "2012-06-24T05:00:00+00:00" 

回答

0

调试后我意识到d是发生了什么事是模型正在initalized这样的:

house = new House() 

因此,created_at属性是不确定的。

我决定用timeago.js来显示信息。但是,此问题的解决方法可能是:

App.House = Backbone.Model.extend({ 
    url: function() { 
     return API_URL + this.id; 
    }, 
    created_at: function() { 
     // further processing could be performed here 
     // before returning. You might want to return a 
     // formatted string or something... 
     return new Date(this.get('created_at')); 

    }, 
}); 
1

您需要以Javascript的形式提供日期Javascript like:

new Date("Month dd, yyyy hh:mm:ss"); 
new Date("Month dd, yyyy"); 
new Date(yy,mm,dd,hh,mm,ss); 
new Date(yy,mm,dd); 
new Date(milliseconds);