我在项目中引入了一个日期选择器,想知道是否有来自angular和material的官方最近组件,也可以在日历中包括时间。
我看过许多材料文档中的时间选择器,并研究了很多第三方选择器,但它们似乎非常复杂。
我在项目中引入了一个日期选择器,想知道是否有来自angular和material的官方最近组件,也可以在日历中包括时间。
我看过许多材料文档中的时间选择器,并研究了很多第三方选择器,但它们似乎非常复杂。
当使用matInput
的类型为datetime-local
时,您可以拥有一个日期时间选择器,例如:
<mat-form-field>
<input matInput type="datetime-local" placeholder="start date">
</mat-form-field>
您可以点击占位符的每个部分来设置日期、月份、年份、小时、分钟以及是上午还是下午。
dd/mm/yyyy --:--
格式显示时间? - mohaxtype="datetime-local"
,它只把它当作普通输入框处理。caniuse 统计数据。 - mhodges我建议你查看@angular-material-components/datetime-picker。 这是一个DatetimePicker,类似于@angular/material Datepicker,支持选择时间。
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格式发送到服务器(或者至少保存时加上正确的时区)。
很遗憾,关于您的问题是否有官方材料支持选择时间的答案是“否”,但这是目前在官方Material2 GitHub存储库中的一个公开问题:https://github.com/angular/material2/issues/5648
希望这很快会改变,同时,您将不得不使用已经发现的第三方支持。在那个GitHub问题中,有一些人提供了他们自己制作的解决方法,您可以尝试一下。
目前有一个官方的Open Issue要求Angular团队支持时间和日期选择器,这个Issue已经存在4年了:
https://github.com/angular/components/issues/5648
有很多库可用,你可以在这篇帖子中看到,但问题是它们中大多数已被弃用(只适用于旧版本的Angular)。
因此,我实际上正在使用@matheo制作的这款日期选择器,它是我找到的最新和最维护的:
https://www.npmjs.com/package/@coachcare,它最近被移动到https://www.npmjs.com/package/@matheo/datepicker
Demo: http://matheo.co/demos/datepicker
NOTE: If it doesn't work for Angular version >= 12 please check that new Angular convention theming is being used, e.g.:
@use "~@matheo/datepicker/theming" as datepicker; // ... @include datepicker.mat-datepicker-theme($main_theme);
@matheo/datepicker
包包含 .mjs
文件,Node.js 将其视为 ECMAScript 模块,但这种方式不受我的应用程序配置支持。我尝试过使用 webpack 模块配置进行调整,但现在已经放弃了。导入该组件会导致数百个错误,例如:
Microsoft.AspNetCore.SpaServices: Error: ERROR in ../node_modules/@matheo/datepicker/core/datetime/index.d.ts:8:21 - error TS2694: Namespace '"C:/projects/app/node_modules/@angular/core/core"' has no exported member '╔╡╔╡FactoryDeclaration'.
- fartwhif实现日期和时间控件的最佳方式是使用输入类型“datetime-local”。
请参考https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
示例代码如下:
<mat-grid-list [cols]="5" gutterSize="20px" rowHeight="70px">
<form *ngIf="dateForm && dateForm !== null" [formGroup]="dateForm">
<mat-grid-tile>
<mat-form-field appearance="outline" floatLabel="auto">
<mat-label>From Date</mat-label>
<input type="datetime-local" matInput name="fromTime" formControlName="fromDate">
</mat-form-field>
</mat-grid-tile>
<mat-grid-tile>
<mat-form-field appearance="outline" floatLabel="auto">
<mat-label>To Date</mat-label>
<input type="datetime-local" matInput name="toTime" formControlName="toDate">
</mat-form-field>
</mat-grid-tile>
</form>
</mat-grid-list>
我认为最好使用Angular Material扩展。它看起来非常有用。 https://ng-matero.github.io/extensions/components/datetimepicker/overview
day-time-picker
的问题?ERROR TypeError: Cannot read property 'locale' of undefined at DayTimeCalendarService.push../node_modules/ng2-date-picker/fesm5/ng2-date-picker.js.DayTimeCalendarService.getConfig (ng2-date-picker.js:999) at DayTimeCalendarComponent.push../node_modules/ng2-date-picker/fesm5/ng2-date-picker.js.DayTimeCalendarComponent.init (ng2-date-picker.js:1691) at DayTimeCalendarComponent.push../node_modules/ng2-date-picker/fesm5/ng2-date-picker.js.DayTimeCalendarComponent.ngOnInit (ng2-date-picker.js:1687)
- Kiratakamoment-with-locales.min.js
文件添加到我的资产文件夹中。 - Kirataka