BSDATEPICKER。与TimePicker,如何删除AM/PM

发布于 2025-01-22 08:24:24 字数 575 浏览 0 评论 0原文

我使用Angular 12和NGX-Bootstrap 7.1.2 当将datepicker与TimePicker一起使用时,如何格式化钟表? 在这种情况下,我想从中删除AM/PM,并允许用户以24小时格式选择时间。

这是我正在使用的代码,

<div class="text">   
    <input id="dpReview"
           name="dpReview"
           type="text"
           required
           #allocationDatepicker
           class="form-control"
           bsDatepicker
           placeholder="Select an assignment date"
           [(ngModel)]="allocDate"
           [bsConfig]="{withTimepicker: true, dateInputFormat: 'MMMM DD YYYY, HH:mm'}"/>
</div>

谢谢

Im using angular 12 and ngx-bootstrap 7.1.2
When using the datepicker with timepicker, how can i format the timepicker?
In this case i want to remove the AM/PM from it and allow the user to select the time in the 24h format.

This is the code I'm using

<div class="text">   
    <input id="dpReview"
           name="dpReview"
           type="text"
           required
           #allocationDatepicker
           class="form-control"
           bsDatepicker
           placeholder="Select an assignment date"
           [(ngModel)]="allocDate"
           [bsConfig]="{withTimepicker: true, dateInputFormat: 'MMMM DD YYYY, HH:mm'}"/>
</div>

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独陪着我 2025-01-29 08:24:24

我也遇到了这个问题,显然,datePicker不支持配置在内部集成的钟贴,即使TimePicker本身实际上确实支持它(甚至具有@input)。

我将Angular 15与

我能够通过某种技巧解决问题来解决我的问题。让我清楚地说,这不是推荐的解决方案,因为它取决于datepicker和timepicker的某些内部功能。如果内部变更(例如私有属性或方法名称),解决方法将停止工作,或者(虽然不可能)可以在datepicker或timepicker中引入故障。
我本人仅使用它来暂时解决这个问题,但最终我将用自定义实施替换datepicker。

DatePicker具有事件发射器开头,可用于执行一些初始化。就我而言,它正在访问其中的钟贴组件,并覆盖负责呈现时间的私人方法。

<input id="dpReview"
       name="dpReview"
       type="text"
       bsDatepicker
       #allocationDatepicker="bsDatepicker"
       (onShown)="initDatepicker(allocationDatepicker)"
       .../>
// TODO fix me properly!
// hack timepicker to show 24h instead of 12h format
initDatepicker(dp: BsDatepickerDirective): void {
  // defer execution until reference to datepicker container is initialized
  setTimeout(() => {
    const timepicker = (dp as any)._datepickerRef.instance.startTimepicker!;
  
    // override rendering method
    const originalMethod = timepicker._renderTime as (value?: string | Date) => void;
    const newMethod = (value?: string | Date) => {
      // set the "showMeridian" property to false and call original method
      timepicker.showMeridian = false;
      originalMethod.call(timepicker, value);
      // reset back to true to avoid an infinite loop
      timepicker.showMeridian = true;
    };
    timepicker._renderTime = newMethod;

    // re-render time in case it's initialized with a 00 or 12+ hour
    timepicker._store.select((state: TimepickerState) => state.value)
      .pipe(take(1))
      .subscribe((value: Date | undefined) => {
        newMethod(value);
        timepicker._cd.markForCheck();
      });
  });
}

如您所见,Showmeridian属性是负责渲染12h格式的属性,请参见 github上的timepicker代码
此骇客仅将其简要介绍为false,然后在渲染后再次返回到true。请注意,我尝试在初始化时尝试设置timepicker.showmeridian = false,但这导致了奇怪的行为,渲染方法被无休止地称为,有效地停止了页面的工作。

虽然此示例有效,但我会长期阻止其使用情况,而建议您实现自己的datepicker(也许只是通过将bootstrap datepicker和timepicker组合起来)或完全使用其他库。

I encountered this issue too, apparently the datepicker doesn't support configuring the timepicker integrated within, even though the timepicker itself actually does support it (an even has an @Input for it).

I use Angular 15 with [email protected].

I was able to resolve my issue with a sort of hack-ish workaround. Let me be clear, this is not a recommended solution as it depends on some internal functionality of both the datepicker and timepicker. If the internals change (e.g. private property or method names) the workaround will stop working or (while improbable) can introduce glitches in the datepicker or timepicker.
I only used it myself to temporarily work around the issue for now, but eventually I will replace the datepicker with a custom implementation.

The datepicker has an event emitter onShown, which can be used to perform some initialization. In my case it's accessing the timepicker component inside it and override a private method responsible for rendering the time.

<input id="dpReview"
       name="dpReview"
       type="text"
       bsDatepicker
       #allocationDatepicker="bsDatepicker"
       (onShown)="initDatepicker(allocationDatepicker)"
       .../>
// TODO fix me properly!
// hack timepicker to show 24h instead of 12h format
initDatepicker(dp: BsDatepickerDirective): void {
  // defer execution until reference to datepicker container is initialized
  setTimeout(() => {
    const timepicker = (dp as any)._datepickerRef.instance.startTimepicker!;
  
    // override rendering method
    const originalMethod = timepicker._renderTime as (value?: string | Date) => void;
    const newMethod = (value?: string | Date) => {
      // set the "showMeridian" property to false and call original method
      timepicker.showMeridian = false;
      originalMethod.call(timepicker, value);
      // reset back to true to avoid an infinite loop
      timepicker.showMeridian = true;
    };
    timepicker._renderTime = newMethod;

    // re-render time in case it's initialized with a 00 or 12+ hour
    timepicker._store.select((state: TimepickerState) => state.value)
      .pipe(take(1))
      .subscribe((value: Date | undefined) => {
        newMethod(value);
        timepicker._cd.markForCheck();
      });
  });
}

As you see, the showMeridian property is the one for responsible for rendering the 12h format, see timepicker code on github.
This hack only briefly sets it to false and then back to true again after the rendering. Note that I tried to just set timepicker.showMeridian = false upon initialization instead, but that resulted in strange behavior and the rendering method being called endlessly, effectivelly stopping the page from working.

While this example works, I discourage its usage long-term and instead recommend either implementing your own datepicker (maybe just by combining the bootstrap datepicker and timepicker yourself) or using a different library altogether.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文