Reputation: 25
The moment.js takes a number as a date. For example, if I input 201, I get "01-01-0201 00:00:00". I want 201 to be displayed if I attempt to edit. My approach is;
if (moment(data, 'DD-MM-YYYY HH:mm:SS').isValid()) {
console.log('data if in date format: ' + angular.toJson(data));
return moment(data).format('MM-DD-YYYY HH:mm:SS');
} else {
console.log('data if not in date format: ' + angular.toJson(data));
return data;
}
Upvotes: 0
Views: 118
Reputation: 929
Looks like this expression
moment(data, 'DD-MM-YYYY HH:mm:SS').isValid()
should return false if data is incomplete, but Moment is very forgiving by default. You may specify the last argument in constructor to enable strict parsing:
moment('201', 'DD-MM-YYYY HH:mm:SS', true).isValid() //false
moment('01-01-2016', 'DD-MM-YYYY HH:mm:SS', true).isValid() //false
moment('01-01-2016 12:00:00', 'DD-MM-YYYY HH:mm:SS', true).isValid() //true
Upvotes: 2