【问题标题】:ng-pick-date picker : How to set date format?ng-pick-date 选择器:如何设置日期格式?
【发布时间】:2018-06-13 09:30:45
【问题描述】:

我想使用选择日期选择器。我想设置日期格式,但不知道该怎么做。那么谁能给我一个如何设置日期格式的例子?

这是我的日期选择器的代码:

<label class="control-label my-label">From Date</label>
<div class="input-group">
  <input tabindex="1" class="form-control" [owlDateTime]="fromDateOfConfirmation" [(ngModel)]="fromDate" name="fromDate" [owlDateTimeTrigger]="fromDateOfConfirmation"
   >
  <span class="input-group-addon trigger" [owlDateTimeTrigger]="fromDateOfConfirmation">
    <span class="fa fa-calendar nopad2 fa-lg"></span>
  </span>
  <owl-date-time [pickerType]="'calendar'" #fromDateOfConfirmation></owl-date-time>
</div>

编辑

这个我已经试过了。

export const MY_NATIVE_FORMATS = {
  parseInput: 'LL LT',
  fullPickerInput: 'LL LT',
  datePickerInput: 'LL',
  timePickerInput: 'LT',
  monthYearLabel: 'MMM YYYY',
  dateA11yLabel: 'LL',
  monthYearA11yLabel: 'MMMM YYYY',
};
providers: [
{ provide: OWL_DATE_TIME_FORMATS, useValue: MY_NATIVE_FORMATS },
],

【问题讨论】:

    标签: angular datepicker date-format


    【解决方案1】:

    您需要创建另一个显示格式化日期值的输入。 在您的 html 中为 [ngModel] 创建一个输入,另一个用于显示格式化的日期值。

    <div class="date-container">
    
     <!-- Invisible input keep ngModel value -->
      <input
              class="shadow-input"
              name="date_time"
              [(ngModel)]="currentDate"
              [owlDateTime]="dt1"
    
      >
      <!-- Trigger owl-datepicker, display formatted date value -->
      <input
              type="text"
              [owlDateTimeTrigger]="dt1"
              placeholder="Date Time"
              [value]="currentDate | dateFilter:dateFormat"
      >
    
      <owl-date-time #dt1></owl-date-time>
    </div>
    

    stackblitz上查看演示

    【讨论】:

    • 丑但聪明:)
    • 最佳答案在这里。 owl-date-time 的文档非常差,并且模块中格式化的 moment.js 解决方案的文档为零,您可以看到每个人都使用相同的 OWL_MOMENT_FORMATS 值,因为不清楚其他值可能是什么样的。就个人而言,我很遗憾使用 owl-date-time,但我很高兴我找到了这个答案
    • 太棒了!你被录用了!
    【解决方案2】:

    你必须通过提供者useValue将自定义对象传递给服务

    export const MY_CUSTOM_FORMATS = {
        parseInput: 'LL LT',
        fullPickerInput: 'LL LT',
        datePickerInput: 'LL',
        timePickerInput: 'LT',
        monthYearLabel: 'MMM YYYY',
        dateA11yLabel: 'LL',
        monthYearA11yLabel: 'MMMM YYYY',
    };
    
    selector: 'app-custom-format-example',
    templateUrl: './custom-format.component.html',
    providers: [ 
        {provide: OWL_DATE_TIME_FORMATS, useValue: MY_CUSTOM_FORMATS},
    ],
    

    查看demo

    【讨论】:

    • 是的,我这样做了,但没有工作。如何从 html 中设置日期格式,例如 [owlDateTime]="fromDateOfConfirmation" ??
    • 我认为他们没有提供输入参数来改变它
    • 但我的问题是哪一个??
    • 我需要这样的06-05-2020 11:22 AM GMT。我试过了,但我只能得到像 +05:30 这样的时区。但我需要像字符串(IST)一样显示它。请对此提供帮助
    • 链接断开时看不到私人聊天或任何形式的解决方案,所以这个答案不是那么有用
    【解决方案3】:

    我想你忘了导入 OwlMomentDateTimeModule

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

    【讨论】:

      【解决方案4】:

      如果你想使用那种类型的格式,你需要使用一个单独的模块,叫做

      OwlMomentDateTimeModule

      但它需要一个额外的 NPM 包(MoementJS

      第一步,安装 MomentJS

      npm install ng-pick-datetime-moment moment --save;
      

      然后你可以使用你的格式

      import { NgModule } from '@angular/core';
      import { OwlDateTimeModule, OWL_DATE_TIME_FORMATS} from 'ng-pick-datetime';
      import { OwlMomentDateTimeModule } from 'ng-pick-datetime-moment';
      
      // See the Moment.js docs for the meaning of these formats:
      // https://momentjs.com/docs/#/displaying/format/
      export const MY_MOMENT_FORMATS = {
          parseInput: 'l LT',
          fullPickerInput: 'l LT',
          datePickerInput: 'l',
          timePickerInput: 'LT',
          monthYearLabel: 'MMM YYYY',
          dateA11yLabel: 'LL',
          monthYearA11yLabel: 'MMMM YYYY',
      };
      
      @NgModule({
          imports: [OwlDateTimeModule, OwlMomentDateTimeModule],
          providers: [
              {provide: OWL_DATE_TIME_FORMATS, useValue: MY_MOMENT_FORMATS},
          ],
      })
      export class AppExampleModule {
      }
      

      作为替代方案您可以使用标准格式化程序

      import { NgModule } from '@angular/core';
      import { OwlDateTimeModule, OwlNativeDateTimeModule, OWL_DATE_TIME_FORMATS} from 'ng-pick-datetime';
      
      // learn more about this from
      // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
      export const MY_NATIVE_FORMATS = {
          fullPickerInput: {year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric'},
          datePickerInput: {year: 'numeric', month: 'numeric', day: 'numeric'},
          timePickerInput: {hour: 'numeric', minute: 'numeric'},
          monthYearLabel: {year: 'numeric', month: 'short'},
          dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
          monthYearA11yLabel: {year: 'numeric', month: 'long'},
      };
      
      @NgModule({
          imports: [OwlDateTimeModule, OwlNativeDateTimeModule],
          providers: [
              {provide: OWL_DATE_TIME_FORMATS, useValue: MY_NATIVE_FORMATS},
          ],
      })
      export class AppExampleModule {
      }
      

      阅读更多关于 Ng 日期时间选择器here

      【讨论】:

        猜你喜欢
        • 2017-04-08
        • 2020-12-17
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多