【问题标题】:How to convert date and time to different timezones in momentjs如何在momentjs中将日期和时间转换为不同的时区
【发布时间】:2018-03-29 09:51:47
【问题描述】:

在我的 Angular 应用程序中,我有一个选择框,用户可以在其中选择他/她想要将特定时间转换到哪个时区。目前我正在使用momentjs进行日期时间操作,问题是我不知道如何动态更改日期时间的时区,因为momentjs只有.utc()方法。

到目前为止,我只使用了一个时区,所以我硬编码了utc() 方法。

我的服务:

public convertDate(): void {
    this.date = moment(this.date).utc().format("DD/MM/YYYY - HH:mm");
}

应该是(伪代码):

public convertDate(selectedTimezone: string): void {
    this.date = moment(this.date).selectedTimezone().format("DD/MM/YYYY - HH:mm");
}

【问题讨论】:

标签: angular momentjs


【解决方案1】:

您可以使用moment-timezone。

  1. 安装时刻时区: npm install --save moment-timezone
  2. 安装类型:npm install -D @types/moment-tipes
  3. 导入到您的服务:import "moment-tipes"

在您的角度服务中:

public convertDate(selectedTimezone: string): void {
    this.date = moment(this.date).tz(selectedTimezone).format("DD/MM/YYYY - HH:mm");
}

【讨论】:

  • 它是:npm install -D @types/moment-timezone
猜你喜欢
  • 1970-01-01
  • 2019-08-31
  • 2020-04-18
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 2019-01-15
  • 1970-01-01
相关资源
最近更新 更多