【问题标题】:convert string to Time format in typescript在打字稿中将字符串转换为时间格式
【发布时间】:2018-04-25 06:04:47
【问题描述】:

我必须将我的服务器数据以字符串格式转换为13:4701:47PM,但我正在尝试使用{{time | date:"hh:MM"}}{{task.time | date: 'shortTime'}},但它显示日期管道错误和参数错误

运行时错误

InvalidPipeArgument:管道“DatePipe”的“13:17”

【问题讨论】:

  • 你能贴出你的代码吗?
  • 日期管道的输入应该是an expression which is a date object or a number (milliseconds since UTC epoch) or an ISO string (https://www.w3.org/TR/NOTE-datetime).

标签: angular typescript ionic-framework ionic3


【解决方案1】:

这里你需要使用momentjs

第一步:

Reference:

npm install moment

ts

import * as moment from 'moment';

第二步:

Reference:

moment({ // Options here }).format('hh:mmA')

【讨论】:

    【解决方案2】:

    来自文档

    https://angular.io/api/common/DatePipe

    表达式是日期对象或数字(自 UTC 纪元以来的毫秒数)或 ISO 字符串 (https://www.w3.org/TR/NOTE-datetime)。

    Angular 默认 DatePipe 仅支持日期对象或 ISOStrings()。如果你想转换成格式'01:47PM',格式将是'hh:mmA'

    【讨论】:

      猜你喜欢
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多