我想只使用年份和月份采摘者进行日期选择器
我需要一个只有一年和一个月的选择器的日期选择器,其中只有一个具有角度材料的材料,但是只有一个带有mm/dd/yyyy的数据范围选择器 - mm/dd/yyyy格式。我想要mm/yyyy - mm/yyyy格式日期范围选择器。
它的ts文件:
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import {MatDatepicker} from '@angular/material/datepicker';
import * as _moment from 'moment';
import {default as _rollupMoment, Moment} from 'moment';
const moment = _rollupMoment || _moment;
export const MY_FORMATS = {
parse: {
dateInput: 'MM/YYYY',
},
display: {
dateInput: 'MM/YYYY',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
/** @title Datepicker emulating a Year and month picker */
@Component({
selector: 'datepicker-views-selection-example',
templateUrl: 'datepicker-views-selection-example.html',
styleUrls: ['datepicker-views-selection-example.css'],
providers: [
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS],
},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
})
export class DatepickerViewsSelectionExample {
date = new FormControl(moment());
setMonthAndYear(normalizedMonthAndYear: Moment, datepicker: MatDatepicker<Moment>) {
const ctrlValue = this.date.value!;
ctrlValue.month(normalizedMonthAndYear.month());
ctrlValue.year(normalizedMonthAndYear.year());
this.date.setValue(ctrlValue);
datepicker.close();
}
}
html:
<mat-form-field appearance="fill">
<mat-label>Month and Year</mat-label>
<input matInput [matDatepicker]="dp" [formControl]="date">
<mat-hint>MM/YYYY</mat-hint>
<mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
<mat-datepicker #dp
startView="multi-year"
(monthSelected)="setMonthAndYear($event, dp)"
panelClass="example-month-picker">
</mat-datepicker>
</mat-form-field>
我不介意如何使其范围选择器
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您会在此处找到一个月/年的选择器的示例
您可以将其与范围选择器结合
You will find an example of month/year picker here
https://material.angular.io/components/datepicker/examples#datepicker-views-selection
you can combine it with a range picker
有了一点工作和一些格式,这是可能的。我们需要实现
my_formats
以将选定的日期格式化为mm/yyyy
。和(noterSelected)=“ chosemonthhandler($ event,dp)”
我们关闭datepickerdatepicker.close.close();
隐藏了picker视图。工作示例: https://stackblitz.com/edit/angular-un1hfx?file=pl=p %2fdatepicker-views-views-selection-example.ts
灵感来自: https://material.angular.io/components/components/datepicker/datepicker/examples.datepicker#datepicker-views-views-views-selection
With a little bit of work and some formatting it is possible. We need to implement
MY_FORMATS
to format selected date toMM/YYYY
. And on(monthSelected)="chosenMonthHandler($event, dp)"
we close datepickerdatepicker.close();
to hide the day picker view.Working example: https://stackblitz.com/edit/angular-un1hfx?file=app%2Fdatepicker-views-selection-example.ts
Inspired by: https://material.angular.io/components/datepicker/examples#datepicker-views-selection