Ember Date获得月,年和日

时间:2022-09-27 20:18:22

I've searched around but I couldn't find any help.

我四处寻找,但找不到任何帮助。

I want to know if I can get the Day, Month, and Year, and maybe other attributes, from Ember's Date attribute.

我想知道我是否可以从Ember的Date属性中获取Day,Month和Year,以及其他属性。

For example, in one of my models, I have something like this:

例如,在我的一个模型中,我有这样的事情:

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

which returns me this format: Tue Feb 23 2016 10:27:10 GMT-0800 (PST)

返回给我这种格式:2016年2月23日星期二10:27:10 GMT-0800(太平洋标准时间)

Is there a way which I can get the "Feb" portion from the whole Date?

有没有办法可以从整个日期获得“二月”部分?

I know in pure JS, you can do something like var today = new Date.getMonth(), but that doesn't work, of course, for Ember's date.

我知道在纯JS中,你可以做一些类似var today = new Date.getMonth()的东西,但这当然不适用于Ember的约会。

Thank you.

谢谢。

1 个解决方案

#1


1  

Actually Your can make it work using new Date.getMonth() as You said in your question. See the following code:

实际上你可以使用你在问题中所说的新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)
    })
});

The next thing You might do is choose a way of converting the number to a month name. Please check this: Get month name from Date

您可能要做的下一件事是选择一种将数字转换为月份名称的方法。请检查:从日期获取月份名称

Cheers.

干杯。

#1


1  

Actually Your can make it work using new Date.getMonth() as You said in your question. See the following code:

实际上你可以使用你在问题中所说的新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)
    })
});

The next thing You might do is choose a way of converting the number to a month name. Please check this: Get month name from Date

您可能要做的下一件事是选择一种将数字转换为月份名称的方法。请检查:从日期获取月份名称

Cheers.

干杯。