Search code examples
mongodbmeteormomentjsmeteor-blaze

how can i take the value of the month of the variable CreatedAt


I have the collection orders, and i want to take month and year of the variable "createdAt" ; i use momentsjs in the app.

Orders = new Mongo.Collection('orders');

Orders.attachSchema(new SimpleSchema({
  'taxtotal':{type:Number}, 
  'subtotal':{type:Number}, 



  createdAt:{
    type: Date,
    autoValue: function() {
       return new Date()
    }
  }, 

  createdBy: {
    type: String,
    autoValue: function() {
       return this.userId
    }
  }

}));

Solution

  • You can try formatting the date using moment and just passing a 'month' or 'year' to to format()

    Example:

    var month = moment(createdAt).format('M');
    var year  = moment(createdAt).format('YYYY');
    

    Or you can use the built in functions to retrieve these values DOCS

    var month = moment(createdAt).month();
    var year  = moment(createdAt).year();