Angular 2 Material 2 datepicker date format

前端 未结 14 2294
悲哀的现实
悲哀的现实 2020-11-28 23:21

I need help. I don\'t know how to change the date format of the material 2 datepicker. I\'ve read documentation but I don\'t understand what I actually need to do. Output da

14条回答
  •  囚心锁ツ
    2020-11-28 23:37

    Robouste worked perfect!!

    I made easy one (Angular 4 "@angular/material": "^2.0.0-beta.10") first made datepicker.module.ts

    
    
        import { NgModule }  from '@angular/core';
        import { MdDatepickerModule, MdNativeDateModule, NativeDateAdapter, DateAdapter, MD_DATE_FORMATS  }  from '@angular/material';
    
        class AppDateAdapter extends NativeDateAdapter {
            format(date: Date, displayFormat: Object): string {
                if (displayFormat === 'input') {
                    const day = date.getDate();
                    const month = date.getMonth() + 1;
                    const year = date.getFullYear();
                    return `${year}-${month}-${day}`;
                } else {
                    return date.toDateString();
                }
            }
        }
    
        const APP_DATE_FORMATS = {
        parse: {
        dateInput: {month: 'short', year: 'numeric', day: 'numeric'}
        },
        display: {
        // dateInput: { month: 'short', year: 'numeric', day: 'numeric' },
        dateInput: 'input',
        monthYearLabel: {year: 'numeric', month: 'short'},
        dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
        monthYearA11yLabel: {year: 'numeric', month: 'long'},
        }
        };
    
        @NgModule({
        declarations:  [ ],
        imports:  [ ],
            exports:  [ MdDatepickerModule, MdNativeDateModule ],
        providers: [
        {
            provide: DateAdapter, useClass: AppDateAdapter
        },
        {
            provide: MD_DATE_FORMATS, useValue: APP_DATE_FORMATS
        }
        ]
        })
    
        export class DatePickerModule {
    
        }
    

    just import it (app.module.ts)

    
        import {Component, NgModule, VERSION,  ReflectiveInjector}   from '@angular/core'//NgZone,
        import { CommonModule }              from '@angular/common';
        import {BrowserModule}               from '@angular/platform-browser'
        import { BrowserAnimationsModule }        from '@angular/platform-browser/animations';
        import { FormsModule }              from '@angular/forms';
        import { DatePickerModule }            from './modules/date.picker/datepicker.module';
    
        @Component({
        selector: 'app-root',
        template: `
        
        
        
        `,
        })
    
    
        export class App{
            datepicker = {SearchDate:new Date()}
            constructor( ) {}
    
            }
    
        @NgModule({
        declarations: [ App ],
        imports: [ CommonModule, BrowserModule, BrowserAnimationsModule, FormsModule, DatePickerModule],
        bootstrap: [ App ],
        providers: [   ]//NgZone
        })
        export class AppModule {}
    
    

提交回复
热议问题