【问题标题】:momentjs - change format of local timemomentjs - 更改当地时间的格式
【发布时间】:2019-02-21 12:24:44
【问题描述】:

我正在使用时刻来获取时间。我收到的格式是:2019 年 2 月 21 日星期四 10:44:21 GMT+0500(巴基斯坦标准时间)。但是,我需要 03:44:21PM 的格式。谁能告诉我如何使用momentjs实现这一目标?

【问题讨论】:

  • moment().format('hh:mm:ssA')
  • 这会在 10:44:21 返回。不添加 5 小时的偏移量。

标签: javascript reactjs timezone momentjs


【解决方案1】:

您可以为此目的使用momentjsmoment().format()

moment().format('hh:mm:ss A')

更多详情请访问docs

本地也可用于转换出固定偏移模式:

moment.parseZone('2016-05-03T22:15:01+02:00').local().format('hh:mm:ss A');

【讨论】:

  • 请看我对 tholle 回答的评论。
  • Local 也可用于转换出固定偏移模式:moment.parseZone('2016-05-03T22:15:01+02:00').local().format(' hh:mm:ss A');
【解决方案2】:

获取您的时区并使用此转换您的时区:

let timeZone = moment.tz.guess();
let date = 'Thu Feb 21 2019 10:44:21 GMT+0500' // input date (other timezone)
let convertDate = moment(date).tz(timeZone) //converted Time

【讨论】:

  • 我收到此错误:“无法读取未定义的属性'猜测'”
  • 试试这个:让 timeZone = moment.tz.setDefault(moment.tz.guess());
  • @Eren555 确保正确导入时刻。使用 es6,它从 import moment from 'moment' 变为 'import moment from 'moment-timezone/builds/moment-timezone-with-data'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多