Change format of md-datepicker in Angular Material with momentjs

前端 未结 13 721
再見小時候
再見小時候 2020-11-28 09:01

Angular material introduced a new date picker component found here.

I want the date returned by this component to be in the format yyy-mm-dd but I am not su

相关标签:
13条回答
  • 2020-11-28 09:54

    To also address the problem pointed out by kazuar:

    Unfortunately it doesn't work if the date is typed from the keyboard

    you should define the parseDate method as well. From the docs:

    $mdDateLocaleProvider.parseDate = function(dateString) {
        var m = moment(dateString, 'L', true);
        return m.isValid() ? m.toDate() : new Date(NaN);
    };
    

    For a full example, I have in my app (using moment):

    $mdDateLocaleProvider.formatDate = function(date) {
        return moment(date).format('DD/MM/YYYY');
    };
    
    $mdDateLocaleProvider.parseDate = function(dateString) {
        var m = moment(dateString, 'DD/MM/YYYY', true);
        return m.isValid() ? m.toDate() : new Date(NaN);
    };
    

    Regards

    0 讨论(0)
提交回复
热议问题