Reputation: 607
I am trying to apply this solution to an Ember.js app. https://stackoverflow.com/a/3224854/2084924 It obviously works in the jsfiddle, but I am not able to implement it correctly in Ember.
I am learning, and probably making an obvious mistake. I've placed the function inside the model and am getting an "NaN" error. A date is passed through an input value in the format of M/D/YYYY. Anyone have experience with dates and ember? Can you see why it would fail to parse the date?
//app/model/task.js
import DS from 'ember-data';
export default DS.Model.extend({
taskname: DS.attr(),
startdate: DS.attr(),
enddate: DS.attr(),
duration: Ember.computed('startdate', 'enddate', function() {
var date1 = new Date('startdate');
var date2 = new Date('enddate');
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
}),
banding: DS.attr()
});
Upvotes: 1
Views: 854
Reputation: 966
try this code
//app/model/task.js
import DS from 'ember-data';
export default DS.Model.extend({
taskname: DS.attr(),
startdate: DS.attr(),
enddate: DS.attr(),
duration: Ember.computed('startdate', 'enddate', function() {
var self = this;
var date1 = new Date(self.get('startdate'));
var date2 = new Date(self.get('enddate'));
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
}),
banding: DS.attr()
});
Upvotes: 1
Reputation: 17622
You are not reading the values from your model, you are just trying to convert the strings "startdate" and "enddate" to dates. It should be new Date(this.get('startdate'));
.
import DS from 'ember-data';
export default DS.Model.extend({
taskname: DS.attr(),
startdate: DS.attr(),
enddate: DS.attr(),
duration: Ember.computed('startdate', 'enddate', function() {
var date1 = new Date(this.get('startdate'));
var date2 = new Date(this.get('enddate'));
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
return diffDays;
}),
banding: DS.attr()
});
Upvotes: 3