【问题标题】:Angular Date pipe to adopt all timezone including ISTAngular Date 管道采用所有时区,包括 IST
【发布时间】:2021-09-14 07:45:58
【问题描述】:

我正在使用 Angular 5 和日期管道来格式化日期。 resDate 是 String 数据类型,rformat 是 Thu Jul 01 12:57:25 CDT 2021。 这需要转换为以下格式

{{resDate |  | date:"MM/dd/yy hh:mm a"} -->  06/22/21 05:35 PM

此代码适用于 CDT 时区,并为 IST 时间抛出错误“InvalidPipeArgument: 'Mon Jun 21 19:01:32 IST 2021' for pipe 'DatePipe'”

Thu Jul 01 12:57:25 CDT 2021 // working
Mon Jun 21 19:01:32 IST 2021 // not working

如何利用日期管道来采用所有时区。如果这不可能,请仅格式化 CDT 时间和离开日期,因为它是 IST 以防止错误。我搜索了很多答案,但找不到正确的方法。

【问题讨论】:

  • 注意:有 3 个不同的标准时间,它们使用 IST 缩写 *只是为了记住 3 和 4 个字母的时间很少是唯一的,并且经常会出现意外

标签: javascript angular date pipe


【解决方案1】:

正如您所提到的,resDate 变量是一个字符串,请查看此处的文档 - https://angular.io/api/common/DatePipe 日期管道要求输入为类型号,即自 1970 年 1 月 1 日 00:00 以来的 Unix 时间(以毫秒为单位) :00 UTC。

@Component({
 selector: 'date-pipe',
 template: `<div>
   <p>Today is {{today | date}}</p>
   <p>Or if you prefer, {{today | date:'fullDate'}}</p>
   <p>The time is {{today | date:'h:mm a z'}}</p>
 </div>`
})
// Get the current date and time as a date-time value.
export class DatePipeComponent {
  today: number = Date.parse('04 Dec 1995 00:12:00 PST');
}

希望这有助于解决您的问题。您只需要将字符串格式化为 Date 对象。

【讨论】:

  • 我还需要把字符串转换成需要的格式
  • 您始终可以使用Date.parse() 并将其传递给模板。我已经编辑了答案以使其更清晰
猜你喜欢
  • 2020-06-10
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多