我在一个项目中导入了一个date picker,想知道是否有来自angular和material的任何官方最近组件,以将时间也包括在日历中。
我在材料文档中看到了大量的时间选择器,并研究了许多第三方的时间选择器,但它们似乎非常复杂。
发布于 2019-02-26 23:39:44
在类型为datetime-local的情况下使用matInput时,可以使用日期时间选择器,如下所示:
<mat-form-field>
<input matInput type="datetime-local" placeholder="start date">
</mat-form-field>您可以单击占位符的每个部分来设置日、月、年、小时、分钟以及是上午还是下午
发布于 2019-04-20 00:27:20
因为只要没有来自angular本身的官方日期和时间选择器,我就建议组合使用default angular date picker和这个Angular Material Timepicker。我之所以选择这个版本,是因为我目前发现的所有其他版本都缺乏对问题的支持,或者已经过时,或者在最新的angular版本中运行得不好。这家伙似乎反应很灵敏。
我将它们都封装在一个组件中,这样看起来就像是一个单元。你只需要确保做几件事:
当还没有提供任何输入时,我会建议:
touchUi = true,以便日期选取器和时间选取器都以对话框的形式出现。在给定值之后,很明显一部分包含时间,另一部分包含日期。此时,很明显,用户必须单击时间来更改时间,并单击日期来更改日期。但在此之前,当两个字段都为空(并且作为一个字段彼此“附加”)时,您应该确保用户不会因为执行上述建议而感到困惑。
我的组件还没有完成,稍后我会试着记住自己来分享代码。如果这个问题已经超过一个月了,那就发表评论吧。
编辑:结果

<div fxLayout="row">
<div *ngIf="!dateOnly" [formGroup]="timeFormGroup">
<mat-form-field>
<input matInput [ngxTimepicker]="endTime" [format]="24" placeholder="{{placeholderTime}}" formControlName="endTime" />
</mat-form-field>
<ngx-material-timepicker #endTime (timeSet)="timeChange($event)" [minutesGap]="10"></ngx-material-timepicker>
</div>
<div>
<mat-form-field>
<input id="pickerId" matInput [matDatepicker]="datepicker" placeholder="{{placeholderDate}}" [formControl]="dateForm"
[min]="config.minDate" [max]="config.maxDate" (dateChange)="dateChange($event)">
<mat-datepicker-toggle matSuffix [for]="datepicker"></mat-datepicker-toggle>
<mat-datepicker #datepicker [disabled]="disabled" [touchUi]="config.touchUi" startView="{{config.startView}}"></mat-datepicker>
</mat-form-field>
</div>
</div>import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { DateAdapter, MatDatepickerInputEvent } from '@angular/material';
import * as moment_ from 'moment';
const moment = moment_;
import { MAT_MOMENT_DATE_ADAPTER_OPTIONS } from '@angular/material-moment-adapter';
class DateConfig {
startView: 'month' | 'year' | 'multi-year';
touchUi: boolean;
minDate: moment_.Moment;
maxDate: moment_.Moment;
}
@Component({
selector: 'cb-datetimepicker',
templateUrl: './cb-datetimepicker.component.html',
styleUrls: ['./cb-datetimepicker.component.scss'],
})
export class DatetimepickerComponent implements OnInit {
@Input() disabled: boolean;
@Input() placeholderDate: string;
@Input() placeholderTime: string;
@Input() model: Date;
@Input() purpose: string;
@Input() dateOnly: boolean;
@Output() dateUpdate = new EventEmitter<Date>();
public pickerId: string = "_" + Math.random().toString(36).substr(2, 9);
public dateForm: FormControl;
public timeFormGroup: FormGroup;
public endTime: FormControl;
public momentDate: moment_.Moment;
public config: DateConfig;
//myGroup: FormGroup;
constructor(private adapter : DateAdapter<any>) { }
ngOnInit() {
this.adapter.setLocale("nl-NL");//todo: configurable
this.config = new DateConfig();
if (this.purpose === "birthday") {
this.config.startView = 'multi-year';
this.config.maxDate = moment().add('year', -15);
this.config.minDate = moment().add('year', -90);
this.dateOnly = true;
} //add more configurations
else {
this.config.startView = 'month';
this.config.maxDate = moment().add('year', 100);
this.config.minDate = moment().add('year', -100);
}
if (window.screen.width < 767) {
this.config.touchUi = true;
}
if (this.model) {
var mom = moment(this.model);
if (mom.isBefore(moment('1900-01-01'))) {
this.momentDate = moment();
} else {
this.momentDate = mom;
}
} else {
this.momentDate = moment();
}
this.dateForm = new FormControl(this.momentDate);
if (this.disabled) {
this.dateForm.disable();
}
this.endTime = new FormControl(this.momentDate.format("HH:mm"));
this.timeFormGroup = new FormGroup({
endTime: this.endTime
});
}
public dateChange(date: MatDatepickerInputEvent<any>) {
if (moment.isMoment(date.value)) {
this.momentDate = moment(date.value);
if (this.dateOnly) {
this.momentDate = this.momentDate.utc(true);
}
var newDate = this.momentDate.toDate();
this.model = newDate;
this.dateUpdate.emit(newDate);
}
console.log("datechange",date);
}
public timeChange(time: string) {
var splitted = time.split(':');
var hour = splitted[0];
var minute = splitted[1];
console.log("time change", time);
this.momentDate = this.momentDate.set('hour', parseInt(hour));
this.momentDate = this.momentDate.set('minute', parseInt(minute));
var newDate = this.momentDate.toDate();
this.model = newDate;
this.dateUpdate.emit(newDate);
}
}一个重要的来源:https://github.com/Agranom/ngx-material-timepicker/issues/126
我认为它仍然值得一些调整,因为我认为当我有更多的时间创建它的时候,它可以工作得更好。最重要的是,我还试图解决UTC问题,因此所有日期都应该以本地时间显示,但应该以UTC格式发送到服务器(或者至少保存时带正确的时区)。
发布于 2020-01-10 19:32:46
我建议你去看看@angular-material-components/datetime-picker。这是一个类似@angular/material Datepicker的DatetimePicker,增加了对选择时间的支持。

https://stackoverflow.com/questions/48649987
复制相似问题