2016-02-26 168 views
0

我搜索了四周,但我找不到任何幫助。Ember日期獲得月份,年份和日期

我想知道是否可以從Ember的日期屬性中獲得Day,Month和Year,以及其他屬性。

例如,在我的車型之一,我有這樣的事情:

createdAt: DS.attr('string', { 
    defaultValue() { 
     return new Date(); 
    } 
}) 

返回我這個格式:週二2016年2月23日10點27分10秒GMT-0800(PST)

有沒有一種方法可以從整個日期中獲取「2月」部分?

我知道在純JS中,你可以做一些像var today = new Date.getMonth(),但是這對Ember的日期當然不起作用。

謝謝。

+0

只需將'Date()'分割爲一個字符串並返回數組中的第二項。 – Ellis

+0

var values = this.get('createdAt')。split(''); \t \t var mm = values [1]; \t \t return mm;此代碼在餘燼中返回空白。 – sallyp

回答

1

其實你可以使用new Date.getMonth()工作,就像你在你的問題中說的那樣。請看下面的代碼:

import DS from 'ember-data'; 
import Ember from 'ember'; 

export default DS.Model.extend({ 
    createdAt: DS.attr('string', { 
     defaultValue() { 
      return new Date(); 
     } 
    }), 
    createdMonth: Ember.computed('createdAt', function(){ 
     return this.get('createdAt').getMonth() + 1 // returns 2 for the current month (February) 
    }) 
}); 

接下來的事情你可以做的是選擇的數量轉換爲一個月的名稱的方式。請檢查:Get month name from Date

乾杯。

+0

非常感謝!一個問題是,Ember Docs是否在其網站上列出了這樣的功能('getMonth,getYear ...,getDate)?或者我們只是使用任何Javascript有? – sallyp

+0

我想我們只是使用任何Javascript有,因爲'日期'來自JS,而不是燼。 –

+0

啊好吧,你有沒有得到這個錯誤:「Uncaught TypeError:this.get(...)。getMonth不是一個函數」?在我的網站上,數據顯示一次,但是如果我轉到另一頁並返回,它會消失。 – sallyp

相關問題