@angular-material-components/datetime-picker

中文文档
16.0.1发布于 2023/08/02MITTypeScriptES Module近 30 天下载 11.7万

Angular Material Datetime Picker

原始使用文档
文档目录10 节
  1. Description
  2. DEMO
  3. Getting started
  4. Setup
  5. Using the component
  6. Datetime Picker (ngx-mat-datetime-picker)
  7. Timepicker (ngx-mat-timepicker)
  8. Choosing a date implementation and date format settings
  9. Theming
  10. License

Angular Material DatetimePicker, Timepicker for @angular/material 7.x, 8.x, 9.x, 10.x, 11.x, 12.x, 13.x, 14.x, 15.x, 16.x

Build Status License npm version Github All Releases

Description

A DatetimePicker like @angular/material Datepicker by adding support for choosing time.

Buy Me A Coffee

DEMO

@see LIVE DEMO AND DOCUMENTATION

@see DEMO stackblitz

Alt Text

Choose the version corresponding to your Angular version:

Angular@angular-material-components/datetime-picker
1616.x+
1515.x+ OR 9.x+ for legacy import
148.x+
137.x+
126.x+
115.x+
104.x+
92.x+
82.x+
72.x+

Getting started

npm install --save  @angular-material-components/datetime-picker

Setup

Basically the same way the @angular/material Datepicker is configured and imported.

import { NgxMatDatetimePickerModule, NgxMatTimepickerModule } from '@angular-material-components/datetime-picker';
@NgModule({
   ...
   imports: [
      BrowserModule,
      HttpClientModule,
      BrowserAnimationsModule,
      MatDatepickerModule,
      MatInputModule,
      NgxMatTimepickerModule,
      FormsModule,
      ReactiveFormsModule,
      MatButtonModule,
      NgxMatDatetimePickerModule,
   ],
   ...
})
export class AppModule { }

@see src/app/demo-datetime/demo-datetime.module.ts

Using the component

The same API as @angular/material Datepicker (@see API docs)

Datetime Picker (ngx-mat-datetime-picker)

<mat-form-field>
   <input matInput [ngxMatDatetimePicker]="picker" placeholder="Choose a date" [formControl]="dateControl"
      [min]="minDate" [max]="maxDate" [disabled]="disabled">
   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <ngx-mat-datetime-picker #picker [showSpinners]="showSpinners" [showSeconds]="showSeconds"
      [stepHour]="stepHour" [stepMinute]="stepMinute" [stepSecond]="stepSecond"
      [touchUi]="touchUi" [color]="color" [enableMeridian]="enableMeridian" 
      [disableMinute]="disableMinute" [hideTime]="hideTime">
   </ngx-mat-datetime-picker>
</mat-form-field>

Timepicker (ngx-mat-timepicker)

<ngx-mat-timepicker [(ngModel)]="date"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [disabled]="disabled"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [stepHour]="2" [stepMinute]="5" [stepSecond]="10"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [showSpinners]="showSpinners"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [showSeconds]="showSeconds"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [disableMinute]="disableMinute"></ngx-mat-timepicker>
<ngx-mat-timepicker [(ngModel)]="date" [defaultTime]="defaultTime"></ngx-mat-timepicker>
<ngx-mat-timepicker [formControl]="formControl"></ngx-mat-timepicker>

List of @Input of ngx-mat-timepicker

You can use all @Input of ngx-mat-timepicker for ngx-mat-datetime-picker

@InputTypeDefault valueDescription
disabledbooleannullIf true, the picker is readonly and can't be modified
showSpinnersbooleantrueIf true, the spinners above and below input are visible
showSecondsbooleantrueIf true, it is not possible to select seconds
disableMinutebooleanfalseIf true, the minute (and second) is readonly
defaultTimeArrayundefinedAn array [hour, minute, second] for default time when the date is not yet defined
stepHournumber1The number of hours to add/substract when clicking hour spinners
stepMinutenumber1The number of minutes to add/substract when clicking minute spinners
stepSecondnumber1The number of seconds to add/substract when clicking second spinners
colorThemePaletteundefinedColor palette to use on the datepicker's calendar.
enableMeridianbooleanfalseWhether to display 12H or 24H mode.
hideTimebooleanfalseIf true, the time is hidden.
touchUibooleanfalseWhether the calendar UI is in touch mode. In touch mode the calendar opens in a dialog rather than a popup and elements have more padding to allow for bigger touch targets.

Choosing a date implementation and date format settings

The datepicker was built to be date implementation agnostic. This means that it can be made to work with a variety of different date implementations. However it also means that developers need to make sure to provide the appropriate pieces for the datepicker to work with their chosen implementation.

The easiest way to ensure this is to import one of the provided date modules:

NgxMatNativeDateModuleNgxMatMomentModule
Date typeDateMoment
Supported localesen-USSee project for details
DependenciesNoneMoment.js
Import from@angular-material-components/datetime-picker@angular-material-components/moment-adapter

To use NgxMatMomentModule:

npm install --save  @angular-material-components/moment-adapter

Please note: NgxMatNativeDateModule is based off the functionality available in JavaScript's native Date object. Thus it is not suitable for many locales. One of the biggest shortcomings of the native Date object is the inability to set the parse format.

We highly recommend using the NgxMatMomentModule or a custom NgxMatDateAdapter that works with the formatting/parsing library of your choice.

For example:

Creating a custom date adapter:

@Injectable()
export class CustomDateAdapter extends NgxMatDateAdapter<D> {...}
// D can be Date, Moment or customized type

Creating a custom date adapter module

@NgModule({
  providers: [
    {
      provide: NgxMatDateAdapter,
      useClass: CustomDateAdapter,
      deps: [MAT_DATE_LOCALE, NGX_MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    }
  ],
})
export class CustomDateModule { }

You can also customize the date format by providing your custom NGX_MAT_DATE_FORMATS in your module.

// If using Moment
const CUSTOM_DATE_FORMATS: NgxMatDateFormats = {
  parse: {
    dateInput: "l, LTS"
  },
  display: {
    dateInput: "l, LTS",
    monthYearLabel: "MMM YYYY",
    dateA11yLabel: "LL",
    monthYearA11yLabel: "MMMM YYYY"
  }
};

//and in the module providers 
providers: [
    { provide: NGX_MAT_DATE_FORMATS, useValue: CUSTOM_MOMENT_FORMATS }
  ]

Theming

<link href="https://fonts.googleapis.com/icon?family=Material+Icons&display=block" rel="stylesheet">

License

MIT

版本与发布记录

@angular-material-components/datetime-picker 共发布 40 个版本,首次发布于 2020/03/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
16.0.1latest2023/08/023 年前
16.0.02023/07/233 年前
15.0.02023/07/233 年前
15.0.0-rc.12023/07/233 年前
15.0.0-beta2023/07/233 年前
9.0.02022/12/013 年前

关于 @angular-material-components/datetime-picker 的常见问题

@angular-material-components/datetime-picker 是什么?

Angular Material Datetime Picker

如何安装 @angular-material-components/datetime-picker?

在项目目录运行 npm install @angular-material-components/datetime-picker,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/platform-browser(^16.0.0)、@angular/common(^16.0.0)、@angular/core(^16.0.0)、@angular/forms(^16.0.0)、@angular/material(^16.0.0)、@angular/cdk(^16.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@angular-material-components/datetime-picker 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@angular-material-components/datetime-picker 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@angular-material-components/datetime-picker 可以商用吗?

@angular-material-components/datetime-picker 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@angular-material-components/datetime-picker 的下载量有多少?

根据 npm 官方下载统计,@angular-material-components/datetime-picker 在 2026/10/06 – 2026/11/04 这 30 天内被下载 117,137 次,日均约 3,905 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@angular-material-components/datetime-picker 还在维护吗?

最新版本 16.0.1 发布于 2023/08/02(3 年前);至今共发布 40 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/03/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。