聊聊自定义angular-datetime-picker格式的方法

怎么自定义angular-datetime-picker格式?下面本篇文章聊聊自定义格式的方法,希望对大家有所帮助!

聊聊自定义angular-datetime-picker格式的方法

最近一直都在使用 Angular 进行开发,维护项目。遇到了日期的问题,同事采用的是 @danielmoncada/angular-datetime-picker。

PS:当然,如果是新项目,还是建议使用框架集成的日期功能,虽然功能可能不是你的预期,但是起码够用。比如 ant designangular 版本。

当然,angular-datetime-picker 提供了很多属性和事件。【相关教程推荐:《angularjs视频教程》】

比如:

owl-date-time 的属性有:

属性名称类型是否必要默认值
pickerTypeboth, calendar, timer可选both
yearOnly布尔值可选false

其他的属性和方法请前往官网查看

当然,本文我们并不是探讨这些简单更改属性和方法的需求。我们来讨论两点:

  • 在输入框中显示 YYYY/MM/ HH:mm:ss 格式

  • 翻译 - 更改按钮的名称 Cancel => 取消Set => 设置

目前默认的值是这样的:

1.png

我们有相关的 html 代码如下:

<ng-container>
  <input 
    element-id="date-time-picker" 
    class="form-control" 
    (ngModelChange)="goToDate($event)" 
    [min]="minDate" [max]="maxDate" 
    [owlDateTimeTrigger]="dt" 
    [(ngModel)]="selectedMoment" 
    [owlDateTime]="dt">
  <owl-date-time #dt [showSecondsTimer]="true"></owl-date-time>
</ng-container>

设置时间格式

app.module.ts 中引入:

import {OwlDateTimeModule, OwlMomentDateTimeModule, OWL_DATE_TIME_FORMATS} from &#39;@danielmoncada/angular-datetime-picker&#39;;

// https://danielykpan.github.io/date-time-picker/#locale-formats
// 自定义格式化时间
export const MY_MOMENT_FORMATS = {
    fullPickerInput: &#39;YYYY/MM/DD HH:mm:ss&#39;, // 指定的时间格式
    datePickerInput: &#39;YYYY/MM/DD&#39;,
    timePickerInput: &#39;HH:mm:ss&#39;,
    monthYearLabel: &#39;YYYY/MM&#39;,
    dateA11yLabel: &#39;YYYY/MM/DD&#39;,
    monthYearA11yLabel: &#39;YYYY/MM&#39;,
};

@NgModule({
  imports: [
    OwlDateTimeModule,
    OwlMomentDateTimeModule
  ],
  providers: [
    {provide: OWL_DATE_TIME_FORMATS, useValue: MY_MOMENT_FORMATS
  ],
})

export class AppModule {
}

得到的结果图如下:

2.png

翻译按钮

我们需要用到这个包的国际化,将对应的 Cancel 翻译成 取消Set 翻译成 设置

官网已经介绍:

import { NgModule } from &#39;@angular/core&#39;; 
import { OwlDateTimeModule, OwlNativeDateTimeModule, OwlDateTimeIntl} from &#39;ng-pick-datetime&#39;; 
// here is the default text string 
export class DefaultIntl extends OwlDateTimeIntl = { 
  /** A label for the up second button (used by screen readers). */
  upSecondLabel= &#39;Add a second&#39;, 
  /** A label for the down second button (used by screen readers). */
  downSecondLabel= &#39;Minus a second&#39;, 
  /** A label for the up minute button (used by screen readers). */ 
  upMinuteLabel= &#39;Add a minute&#39;, 
  /** A label for the down minute button (used by screen readers). */ 
  downMinuteLabel= &#39;Minus a minute&#39;,
  /** A label for the up hour button (used by screen readers). */ 
  upHourLabel= &#39;Add a hour&#39;, 
  /** A label for the down hour button (used by screen readers). */
  downHourLabel= &#39;Minus a hour&#39;, 
  /** A label for the previous month button (used by screen readers). */
  prevMonthLabel= &#39;Previous month&#39;, 
  /** A label for the next month button (used by screen readers). */
  nextMonthLabel= &#39;Next month&#39;, 
  /** A label for the previous year button (used by screen readers). */
  prevYearLabel= &#39;Previous year&#39;, 
  /** A label for the next year button (used by screen readers). */
  nextYearLabel= &#39;Next year&#39;, 
  /** A label for the previous multi-year button (used by screen readers). */
  prevMultiYearLabel= &#39;Previous 21 years&#39;, 
  /** A label for the next multi-year button (used by screen readers). */
  nextMultiYearLabel= &#39;Next 21 years&#39;, 
  /** A label for the &#39;switch to month view&#39; button (used by screen readers). */
  switchToMonthViewLabel= &#39;Change to month view&#39;, 
  /** A label for the &#39;switch to year view&#39; button (used by screen readers). */
  switchToMultiYearViewLabel= &#39;Choose month and year&#39;, 
  /** A label for the cancel button */ 
  cancelBtnLabel= &#39;Cancel&#39;, 
  /** A label for the set button */ 
  setBtnLabel= &#39;Set&#39;, 
  /** A label for the range &#39;from&#39; in picker info */ 
  rangeFromLabel= &#39;From&#39;, 
  /** A label for the range &#39;to&#39; in picker info */ 
  rangeToLabel= &#39;To&#39;, 
  /** A label for the hour12 button (AM) */ 
  hour12AMLabel= &#39;AM&#39;, 
  /** A label for the hour12 button (PM) */ 
  hour12PMLabel= &#39;PM&#39;, 
}; 

@NgModule({  
 imports: [
   OwlDateTimeModule, 
   OwlNativeDateTimeModule
 ], 
 providers: [ 
   {provide: OwlDateTimeIntl, useClass: DefaultIntl}, 
 ], 
}) 

export class AppExampleModule { }

我们按照上面的思路整合下来实现我们的需求:

新建翻译文件 owl-date-time-translator.ts

import { Injectable } from &#39;@angular/core&#39;;
import { DefaultTranslationService } from &#39;@services/translation.service&#39;;
import { OwlDateTimeIntl } from &#39;@danielmoncada/angular-datetime-picker&#39;;

@Injectable()
export class OwlDateTimeTranslator extends OwlDateTimeIntl {

  constructor(protected translationService: DefaultTranslationService) {
    super();

    /** 取消按钮 */
    this.cancelBtnLabel = this.translationService.translate(&#39;action.cancel&#39;);

    /** 设置按钮 */
    this.setBtnLabel = this.translationService.translate(&#39;action.set&#39;);
  }

};

这里我们引入了翻译服务 translationService,可以根据不同地区进行语言选择。

然后我们在 app.module.ts 上操作:

import { OwlDateTimeIntl } from &#39;@danielmoncada/angular-datetime-picker&#39;;

// 翻译 @danielmoncada/angular-datetime-picker
import { OwlDateTimeTranslator } from &#39;./path/to/owl-date-time-translator&#39;;

@NgModule({
  providers: [
    {provide: OwlDateTimeIntl, useClass: OwlDateTimeTranslator},
  ],
})

export class AppModule {
}

得到的效果图如下:

3.png

更多编程相关知识,请访问:编程视频!!

以上就是聊聊自定义angular-datetime-picker格式的方法的详细内容,更多请关注https://www.sxiaw.com/其它相关文章!