0

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.

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)

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.

Thank you.

sallyp
  • 226
  • 3
  • 12
  • Just split the `Date()` as a string and return the second item in the array. – Ellis Feb 26 '16 at 19:42
  • var values = this.get('createdAt').split(' '); var mm = values[1]; return mm; this code returns me a blank in ember. – sallyp Feb 26 '16 at 19:56

1 Answers1

1

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

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.

Community
  • 1
  • 1
Dan Cantir
  • 2,915
  • 14
  • 24
  • Thanks so much! One question is, did Ember Docs list functions like that ('getMonth, getYear..., getDate) on their website? Or do we just use whatever Javascript has? – sallyp Feb 26 '16 at 20:12
  • I think we just use whatever Javascript has, because `Date` is comming from JS, not ember. – Dan Cantir Feb 26 '16 at 20:13
  • Ah okay, have you ever gotten this error: "Uncaught TypeError: this.get(...).getMonth is not a function"? On my website, the data displays once but if I go to another page and go back to it, it disappears. – sallyp Feb 26 '16 at 20:53