【问题标题】:how to change the time format in ionic 3 typescript如何更改 ionic 3 typescript 中的时间格式
【发布时间】:2020-04-05 12:40:55
【问题描述】:

我有一个 15:12:00 (HH:MM:SS) 格式的时间。但我需要以(3.12 PM)格式更改它

 <p class="headings" display-format="HH:mm" > <b>Time :</b> {{this.starttime}} </p>

在打字稿中

 this.starttime = '15:12:00';

【问题讨论】:

  • 我认为你应该使用momentjs。参考:stackoverflow.com/questions/48682405/…
  • 你能有完整的日期时间对象还是只有时间字符串?
  • 我不需要当前时间..我已经在字符串 @SuperBob 中有时间
  • 只是时间字符串@Naseer

标签: html typescript datetime ionic-framework ionic3


【解决方案1】:

此外,您可以参考外部库 https://momentjs.com/ 了解高级日期时间格式化技术。

【讨论】:

    【解决方案2】:

    您可以通过在时间字符串前面加上“0000-00-00T”来实现这一点。然后使用 Angular 的内置日期管道和短时间过滤器。

    {{'0000-00-00T15:12:00' | date:'shortTime'}}
    

    Angular 内置date 管道的官方文档。

    【讨论】:

      【解决方案3】:

      你也可以自己编码:

      function formatTime(date:string): string {
        const split = date.split(':');
        const meridiem = +split[0] > 12 ? 'PM' : 'AM';
        let hours = +split[0] % 12;
        hours = hours ? hours : 12; 
      
        return `${hours}.${split[1]}${meridiem}`;
      }
      
      this.starttime = formatTime('15:12:00');
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-20
        • 1970-01-01
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        • 2018-02-17
        • 2015-03-24
        相关资源
        最近更新 更多