【问题标题】:Stop moment.js from converting time automatically based on local timezone阻止 moment.js 根据本地时区自动转换时间
【发布时间】:2022-12-12 23:58:45
【问题描述】:

我有这种格式的日期2022-12-03T22:25:00.000+13

我用 moment 把这个日期转换成这种格式 moment(value).format('HH:mm') 问题是当我转换它时,结果是10:25 而不是22:25。我希望结果是 22:25。我怎样才能做到这一点?

【问题讨论】:

  • 您确定您使用的是HH 而不是hh 吗?
  • 是的,非常确定。即使我使用 console.log(moment(value)),输出也是 2022-12-03T09:25:00.000Z。如您所见,时间是 21:25 到 09:25

标签: typescript react-native momentjs


【解决方案1】:

您不再需要使用 moment.js,因为 Intl.DateTimeFormat 现已加入。

const date = new Date("2020-05-12T23:50:21.817Z");

let options = {};
options.hour = '2-digit'
options.minute = '2-digit'
options.hourCycle = "h24";
console.log( new Intl.DateTimeFormat('en-US',options).format(date))

【讨论】:

  • 值 2022-12-03T09:25:00.000Z 来自后端作为字符串。因此,为了适应您的实施,我必须将日期作为 console.log( new Intl.DateTimeFormat('en-US',options).format(new Date(2022-12-03T22:25:00.000Z)))因为您的实现不接受仅字符串日期值。这将返回 10:25 而不是 22:25,这不是所需的结果。
  • 只需使用new Date(myString) 解析它。 .hourCycle 选项看起来正是您所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-14
  • 1970-01-01
  • 2017-04-04
  • 2015-08-27
  • 2015-05-20
  • 2017-08-24
  • 1970-01-01
相关资源
最近更新 更多